Add and Remove in Array
Add in Array
In Python, we can add elements to an array. We can add elements to the end of an array using the append()
append()
method. We can also add elements to the beginning of an array using the insert()
insert()
method.
append() Method
The append()
append()
method adds an element to the end of an array. The syntax of the append()
append()
method is as follows:
array_name.append(element)
array_name.append(element)
The append()
append()
method takes one argument, which is the element to be added to the array. The append()
append()
method adds the element to the end of the array.
Let’s see an example of the append()
append()
method.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.append(6)
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.append(6)
print(my_array)
Output:
C:\Users\username>python array_append.py
array('i', [1, 2, 3, 4, 5, 6])
C:\Users\username>python array_append.py
array('i', [1, 2, 3, 4, 5, 6])
In the above example, we create an array of five elements and add a sixth element to the end of the array using the append()
append()
method.
insert() Method
The insert()
insert()
method adds an element to the beginning of an array. The syntax of the insert()
insert()
method is as follows:
array_name.insert(index, element)
array_name.insert(index, element)
The insert()
insert()
method takes two arguments. The first argument is the index at which the element is to be inserted. The second argument is the element to be inserted.
Let’s see an example of the insert()
insert()
method.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.insert(0, 0)
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.insert(0, 0)
print(my_array)
Output:
C:\Users\username>python array_insert.py
array('i', [0, 1, 2, 3, 4, 5])
C:\Users\username>python array_insert.py
array('i', [0, 1, 2, 3, 4, 5])
In the above example, we create an array of five elements and add a sixth element to the beginning of the array using the insert()
insert()
method.
Another example of the insert()
insert()
method.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.insert(3, 6)
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.insert(3, 6)
print(my_array)
Output:
C:\Users\username>python array_insert.py
array('i', [1, 2, 3, 6, 4, 5])
C:\Users\username>python array_insert.py
array('i', [1, 2, 3, 6, 4, 5])
In the above example, we create an array of five elements and add a sixth element to the fourth index of the array using the insert()
insert()
method.
extend() Method
The extend()
extend()
method adds multiple elements to the end of an array. The syntax of the extend()
extend()
method is as follows:
array_name.extend(iterable)
array_name.extend(iterable)
The extend()
extend()
method takes one argument, which is an iterable. The extend()
extend()
method adds all the elements of the iterable to the end of the array.
Let’s see an example of the extend()
extend()
method.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.extend([6, 7, 8])
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.extend([6, 7, 8])
print(my_array)
Output:
C:\Users\username>python array_extend.py
array('i', [1, 2, 3, 4, 5, 6, 7, 8])
C:\Users\username>python array_extend.py
array('i', [1, 2, 3, 4, 5, 6, 7, 8])
In the above example, we create an array of five elements and add three more elements to the end of the array using the extend()
extend()
method.
+= Operator
The +=
+=
operator adds multiple elements to the end of an array. The syntax of the +=
+=
operator is as follows:
array_name += iterable
array_name += iterable
The +=
+=
operator takes one argument, which is an iterable. The +=
+=
operator adds all the elements of the iterable to the end of the array.
Let’s see an example of the +=
+=
operator.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array2 = arr.array('i', [6, 7, 8])
my_array += my_array2
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array2 = arr.array('i', [6, 7, 8])
my_array += my_array2
print(my_array)
Output:
C:\Users\username>python array_extend.py
array('i', [1, 2, 3, 4, 5, 6, 7, 8])
C:\Users\username>python array_extend.py
array('i', [1, 2, 3, 4, 5, 6, 7, 8])
In the above example, we create an array of five elements and add three more elements to the end of the array using the +=
+=
operator.
Remove in Array
In Python, we can remove elements from an array. We can remove elements from the end of an array using the pop()
pop()
method. We can also remove elements from the beginning of an array using the pop()
pop()
method.
pop() Method
The pop()
pop()
method removes an element from array. It takes one argument, which is the index of the element to be removed. Default is the last element. It returns the removed element. The syntax of the pop()
pop()
method is as follows:
element = array_name.pop(index=-1)
element = array_name.pop(index=-1)
The pop()
pop()
method takes one argument, which is the index of the element to be removed. Default is the last element. The pop()
pop()
method returns the removed element.
Let’s see an example of the pop()
pop()
method.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
element = my_array.pop()
print(element)
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
element = my_array.pop()
print(element)
print(my_array)
Output:
C:\Users\username>python array_pop.py
5
array('i', [1, 2, 3, 4])
C:\Users\username>python array_pop.py
5
array('i', [1, 2, 3, 4])
In the above example, we create an array of five elements and remove the last element from the array using the pop()
pop()
method.
Another example of the pop()
pop()
method.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
element = my_array.pop(3)
print(element)
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
element = my_array.pop(3)
print(element)
print(my_array)
Output:
C:\Users\username>python array_pop.py
4
array('i', [1, 2, 3, 5])
C:\Users\username>python array_pop.py
4
array('i', [1, 2, 3, 5])
In the above example, we create an array of five elements and remove the fourth element from the array using the pop()
pop()
method.
remove() Method
The remove()
remove()
method removes an element from array. It takes one argument, which is the element to be removed. It returns None
None
. The syntax of the remove()
remove()
method is as follows:
array_name.remove(element)
array_name.remove(element)
The remove()
remove()
method takes one argument, which is the element to be removed. The remove()
remove()
method returns None
None
.
Let’s see an example of the remove()
remove()
method.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.remove(3)
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.remove(3)
print(my_array)
Output:
C:\Users\username>python array_remove.py
array('i', [1, 2, 4, 5])
C:\Users\username>python array_remove.py
array('i', [1, 2, 4, 5])
In the above example, we create an array of five elements and remove the third element from the array using the remove()
remove()
method.
del Statement
The del
del
statement removes an element from array. It takes one argument, which is the index of the element to be removed. The syntax of the del
del
statement is as follows:
del array_name[index]
del array_name[index]
The del
del
statement takes one argument, which is the index of the element to be removed.
Let’s see an example of the del
del
statement.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
del my_array[3]
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
del my_array[3]
print(my_array)
Output:
C:\Users\username>python array_del.py
array('i', [1, 2, 3, 5])
C:\Users\username>python array_del.py
array('i', [1, 2, 3, 5])
In the above example, we create an array of five elements and remove the fourth element from the array using the del
del
statement.
clear() Method
The clear()
clear()
method removes all elements from array. It takes no argument. It returns None
None
. The syntax of the clear()
clear()
method is as follows:
array_name.clear()
array_name.clear()
The clear()
clear()
method takes no argument. The clear()
clear()
method returns None
None
.
Let’s see an example of the clear()
clear()
method.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.clear()
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.clear()
print(my_array)
Output:
C:\Users\username>python array_clear.py
array('i')
C:\Users\username>python array_clear.py
array('i')
In the above example, we create an array of five elements and remove all elements from the array using the clear()
clear()
method.
Update in Array
In Python, we can update elements in an array. We can update elements in an array using their indices.
Update by Index
In Python, we can update elements in an array using their indices. We can update elements in an array using their indices. The syntax of updating an element in an array is as follows:
array_name[index] = new_value
array_name[index] = new_value
The =
=
operator is used to update an element in an array. The =
=
operator takes two arguments. The first argument is the index of the element to be updated. The second argument is the new value of the element.
Let’s see an example of updating an element in an array.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array[3] = 6
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array[3] = 6
print(my_array)
Output:
C:\Users\username>python array_update.py
array('i', [1, 2, 3, 6, 5])
C:\Users\username>python array_update.py
array('i', [1, 2, 3, 6, 5])
In the above example, we create an array of five elements and update the fourth element of the array.
Update by Slice
In Python, we can update elements in an array using their indices. We can update elements in an array using their indices. The syntax of updating an element in an array is as follows:
array_name[start_index:end_index] = new_value
array_name[start_index:end_index] = new_value
The =
=
operator is used to update an element in an array. The =
=
operator takes two arguments. The first argument is the slice of the array to be updated. The second argument is the new value of the slice.
Let’s see an example of updating an element in an array.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array[1:3] = arr.array('i', [6, 7])
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array[1:3] = arr.array('i', [6, 7])
print(my_array)
Output:
C:\Users\username>python array_update.py
array('i', [1, 6, 7, 4, 5])
C:\Users\username>python array_update.py
array('i', [1, 6, 7, 4, 5])
In the above example, we create an array of five elements and update the second and third elements of the array.
Update by Multiple number
In Python, we can multiply elements in an array using their indices. We can multiply elements in an array using their indices. The syntax of multiplying an element in an array is as follows:
array_name[index] *= number
array_name[index] *= number
The *=
*=
operator is used to multiply array. The *=
*=
operator repeats the element in the array by the number of times specified by the number.
Let’s see an example of multiplying array.
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array *= 2
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array *= 2
print(my_array)
Output:
C:\Users\username>python array_update.py
array('i', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
C:\Users\username>python array_update.py
array('i', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
In the above example, we create an array of five elements and multiply the array by 2.
Conclusion
In this tutorial, we learned how to add and remove elements in an array. We also learned how to add and remove elements at the beginning and end of an array. We also learned how to update elements in an array. We also learned how to update elements in an array using their indices. We also learned how to multiply elements in an array using their indices. For more tutorials like this check out Python Central Hub.
Was this page helpful?
Let us know how we did