Python - Append Item to Array
Append Item to Array in Python
In Python, you can append an item to given array using append() method of the array instance.
In this tutorial, you will learn how to append an item to given Python array, with examples.
To append an item item_1 to given array my_array in Python, call the append() method on the array object, and pass the item as argument to the append() method as shown in the following code snippet.
my_array.append(item)
The array is modified in place, and the given item is appended to end of the array.
1. Append Item to Integer Array
In the following program, we take an integer array my_array with some initial values, and then use append() method to append some more integer items to the array.
Python Program
import array as arr
my_array = arr.array('i', [100, 200, 300, 400])
my_array.append(500)
my_array.append(600)
my_array.append(700)
print(my_array)
Output
array('i', [100, 200, 300, 400, 500, 600, 700])
2. Append Item to Character Array
In the following program, we take a character array my_array with some initial values, and then use append() method to append some more character items to the array.
Python Program
import array as arr
my_array = arr.array('u', 'apple')
my_array.append('b')
my_array.append('a')
my_array.append('n')
my_array.append('a')
my_array.append('n')
my_array.append('a')
print(my_array)
Output
array('u', 'applebanana')
Summary
In this Python Array tutorial, we learned how to append an item to the end of the array in Python, using append(), with examples.