Skip to content

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:

Syntax
array_name.append(element)
Syntax
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.

array_append.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.append(6)
print(my_array)
array_append.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.append(6)
print(my_array)

Output:

command
C:\Users\username>python array_append.py
array('i', [1, 2, 3, 4, 5, 6])
command
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:

Syntax
array_name.insert(index, element)
Syntax
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.

array_insert.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.insert(0, 0)
print(my_array)
array_insert.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.insert(0, 0)
print(my_array)

Output:

command
C:\Users\username>python array_insert.py
array('i', [0, 1, 2, 3, 4, 5])
command
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.

array_insert.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.insert(3, 6)
print(my_array)
array_insert.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.insert(3, 6)
print(my_array)

Output:

command
C:\Users\username>python array_insert.py
array('i', [1, 2, 3, 6, 4, 5])
command
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:

Syntax
array_name.extend(iterable)
Syntax
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.

array_extend.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.extend([6, 7, 8])
print(my_array)
array_extend.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.extend([6, 7, 8])
print(my_array)

Output:

command
C:\Users\username>python array_extend.py
array('i', [1, 2, 3, 4, 5, 6, 7, 8])
command
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:

Syntax
array_name += iterable
Syntax
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.

array_extend.py
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)
array_extend.py
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:

command
C:\Users\username>python array_extend.py
array('i', [1, 2, 3, 4, 5, 6, 7, 8])
command
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:

Syntax
element = array_name.pop(index=-1)
Syntax
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.

array_pop.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
element = my_array.pop()
print(element)
print(my_array)
array_pop.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
element = my_array.pop()
print(element)
print(my_array)

Output:

command
C:\Users\username>python array_pop.py
5
array('i', [1, 2, 3, 4])
command
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.

array_pop.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
element = my_array.pop(3)
print(element)
print(my_array)
array_pop.py
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:

command
C:\Users\username>python array_pop.py
4
array('i', [1, 2, 3, 5])
command
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 NoneNone. The syntax of the remove()remove() method is as follows:

Syntax
array_name.remove(element)
Syntax
array_name.remove(element)

The remove()remove() method takes one argument, which is the element to be removed. The remove()remove() method returns NoneNone.

Let’s see an example of the remove()remove() method.

array_remove.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.remove(3)
print(my_array)
array_remove.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.remove(3)
print(my_array)

Output:

command
C:\Users\username>python array_remove.py
array('i', [1, 2, 4, 5])
command
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 deldel statement removes an element from array. It takes one argument, which is the index of the element to be removed. The syntax of the deldel statement is as follows:

Syntax
del array_name[index]
Syntax
del array_name[index]

The deldel statement takes one argument, which is the index of the element to be removed.

Let’s see an example of the deldel statement.

array_del.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
del my_array[3]
print(my_array)
array_del.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
del my_array[3]
print(my_array)

Output:

command
C:\Users\username>python array_del.py
array('i', [1, 2, 3, 5])
command
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 deldel statement.

clear() Method

The clear()clear() method removes all elements from array. It takes no argument. It returns NoneNone. The syntax of the clear()clear() method is as follows:

Syntax
array_name.clear()
Syntax
array_name.clear()

The clear()clear() method takes no argument. The clear()clear() method returns NoneNone.

Let’s see an example of the clear()clear() method.

array_clear.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.clear()
print(my_array)
array_clear.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.clear()
print(my_array)

Output:

command
C:\Users\username>python array_clear.py
array('i')
command
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:

Syntax
array_name[index] = new_value
Syntax
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.

array_update.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array[3] = 6
print(my_array)
array_update.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array[3] = 6
print(my_array)

Output:

command
C:\Users\username>python array_update.py
array('i', [1, 2, 3, 6, 5])
command
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:

Syntax
array_name[start_index:end_index] = new_value
Syntax
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.

array_update.py
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)
array_update.py
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:

command
C:\Users\username>python array_update.py
array('i', [1, 6, 7, 4, 5])
command
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:

Syntax
array_name[index] *= number
Syntax
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.

array_update.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array *= 2
print(my_array)
array_update.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array *= 2
print(my_array)

Output:

command
C:\Users\username>python array_update.py
array('i', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
command
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