Add and Remove in Array
Add in Array
Section titled “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() method. We can also add elements to the beginning of an array using the insert() method.
append() Method
Section titled “append() Method”The append() method adds an element to the end of an array. The syntax of the append() method is as follows:
array_name.append(element)The append() method takes one argument, which is the element to be added to the array. The append() method adds the element to the end of the array.
Let’s see an example of the append() method.
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])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() method.
insert() Method
Section titled “insert() Method”The insert() method adds an element to the beginning of an array. The syntax of the insert() method is as follows:
array_name.insert(index, element)The 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() method.
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])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() method.
Another example of the insert() method.
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])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() method.
extend() Method
Section titled “extend() Method”The extend() method adds multiple elements to the end of an array. The syntax of the extend() method is as follows:
array_name.extend(iterable)The extend() method takes one argument, which is an iterable. The extend() method adds all the elements of the iterable to the end of the array.
Let’s see an example of the 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)Output:
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() method.
+= Operator
Section titled “+= Operator”The += operator adds multiple elements to the end of an array. The syntax of the += operator is as follows:
array_name += iterableThe += 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)Output:
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
Section titled “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() method. We can also remove elements from the beginning of an array using the pop() method.
pop() Method
Section titled “pop() Method”The 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() method is as follows:
element = array_name.pop(index=-1)The pop() method takes one argument, which is the index of the element to be removed. Default is the last element. The pop() method returns the removed element.
Let’s see an example of the 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)Output:
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() method.
Another example of the 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)Output:
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() method.
remove() Method
Section titled “remove() Method”The remove() method removes an element from array. It takes one argument, which is the element to be removed. It returns None. The syntax of the remove() method is as follows:
array_name.remove(element)The remove() method takes one argument, which is the element to be removed. The remove() method returns None.
Let’s see an example of the remove() method.
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])In the above example, we create an array of five elements and remove the third element from the array using the remove() method.
del Statement
Section titled “del Statement”The 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 statement is as follows:
del array_name[index]The del statement takes one argument, which is the index of the element to be removed.
Let’s see an example of the del statement.
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])In the above example, we create an array of five elements and remove the fourth element from the array using the del statement.
clear() Method
Section titled “clear() Method”The clear() method removes all elements from array. It takes no argument. It returns None. The syntax of the clear() method is as follows:
array_name.clear()The clear() method takes no argument. The clear() method returns None.
Let’s see an example of the clear() method.
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')In the above example, we create an array of five elements and remove all elements from the array using the clear() method.
Update in Array
Section titled “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
Section titled “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_valueThe = 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)Output:
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
Section titled “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_valueThe = 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)Output:
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
Section titled “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] *= numberThe *= 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)Output:
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
Section titled “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.
flowchart TD
A["a = array('b') -- signed char, -128..127"] --> B["a.append(x)"]
B --> C{"is x an integer?"}
C -->|no| D["TypeError: cannot be interpreted as an integer"]
C -->|yes| E{"does it fit the range?"}
E -->|no| F["OverflowError: signed char is greater than maximum"]
E -->|yes| G["stored in itemsize bytes"]
H["a list"] --> I["accepts 128 and 'x' alike"]
I --> J["no check, because each element is a full object"]
Check yourself
Section titled “Check yourself”-
`array('b')` holds signed chars. What does `.append(128)` raise?
Verified: `OverflowError: signed char is greater than maximum`. It does not wrap or truncate — the range is enforced.
pch.quizShowAnswer
C — `OverflowError` — Verified: `OverflowError: signed char is greater than maximum`. It does not wrap or truncate — the range is enforced.
-
What does `array('b').append('x')` raise?
A different exception for a different rule: `TypeError` for the wrong type, `OverflowError` for the right type out of range. Catching only one misses half the cases.
pch.quizShowAnswer
B — `TypeError` — A different exception for a different rule: `TypeError` for the wrong type, `OverflowError` for the right type out of range. Catching only one misses half the cases.
-
Why can a list accept both `128` and `'x'` where an array cannot?
That is the same fact that makes a list about 8.6x larger for 1000 integers. The array's check is what buys the compact fixed-width storage.
pch.quizShowAnswer
B — A list stores references to full objects, each carrying its own type — That is the same fact that makes a list about 8.6x larger for 1000 integers. The array's check is what buys the compact fixed-width storage.
-
Which mutating method does an array NOT have?
Verified against `dir()`: `sort` and `copy` are the only list methods absent. Use `array(a.typecode, sorted(a))` to get a sorted array back.
pch.quizShowAnswer
D — `sort` — Verified against `dir()`: `sort` and `copy` are the only list methods absent. Use `array(a.typecode, sorted(a))` to get a sorted array back.
Try it: Add and Remove Array Exercises
Section titled “Try it: Add and Remove Array Exercises”Exercise 1 – append() and insert()
Section titled “Exercise 1 – append() and insert()”Exercise 2 – remove() and pop()
Section titled “Exercise 2 – remove() and pop()”Exercise 3 – Extend Array
Section titled “Exercise 3 – Extend Array”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading