Skip to content

Array Methods

Array Methods

The array methods are the built-in methods in Python. These methods are used to perform different operations on the array. In this tutorial, we will learn how to use the array methods in Python with the help of examples.

Table of Methods

There are various methods available in Python to perform different operations on the array. The following table lists some of the important methods available in Python.

S.No.MethodDescriptionExample
1append()Adds an element at the end of the arrayarr.append(5)arr.append(5)
2insert()Adds an element at the specified positionarr.insert(2, 5)arr.insert(2, 5)
3remove()Removes the first occurrence of the element with the specified valuearr.remove(5)arr.remove(5)
4pop()Removes the element at the specified positionarr.pop(2)arr.pop(2)
5clear()Removes all the elements from the arrayarr.clear()arr.clear()
6index()Returns the index of the first element with the specified valuearr.index(5)arr.index(5)
7count()Returns the number of elements with the specified valuearr.count(5)arr.count(5)
8sort()Sorts the arrayarr.sort()arr.sort()
9reverse()Reverses the order of the arrayarr.reverse()arr.reverse()
10copy()Returns a copy of the arrayarr.copy()arr.copy()
11extend()Adds the elements of an array to the end of the current arrayarr.extend(arr2)arr.extend(arr2)
12fromlist()Appends the contents of the list to the arrayarr.fromlist(list)arr.fromlist(list)
13tolist()Converts the array to an ordinary list with the same itemsarr.tolist()arr.tolist()
14tofile()Writes all the array elements to a filearr.tofile(f)arr.tofile(f)
15typecode()Returns the type code of the arrayarr.typecodearr.typecode
16itemsize()Returns the size of each element of the array in bytesarr.itemsizearr.itemsize
17buffer_info()Returns a tuple containing the address in which array is stored and the number of elements in itarr.buffer_info()arr.buffer_info()
18byteswap()Reverses the order of the bytes in the arrayarr.byteswap()arr.byteswap()
19frombytes()Appends bytes from the string to the arrayarr.frombytes(s)arr.frombytes(s)
20fromfile()Reads the array from the file, according to the dtypearr.fromfile(f, n)arr.fromfile(f, n)
21fromstring()Appends items from the string into the arrayarr.fromstring(s)arr.fromstring(s)
22fromunicode()Appends items from the unicode string into the arrayarr.fromunicode(s)arr.fromunicode(s)
23tobytes()Converts the array to an array of machine values and returns the bytes representationarr.tobytes()arr.tobytes()

append() Method

The append()append() method adds an element at the end of the array. The syntax of the append()append() method is:

append.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.append(6)
 
print(my_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 append.py
array('i', [1, 2, 3, 4, 5, 6])
command
C:\Users\username>python append.py
array('i', [1, 2, 3, 4, 5, 6])

In the above example, we create an array of five elements and append a new element at the end of the array. The new element is added at the end of the array.

insert() Method

The insert()insert() method adds an element at the specified position. The syntax of the insert()insert() method is:

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

Output:

command
C:\Users\username>python insert.py
array('i', [1, 2, 6, 3, 4, 5])
command
C:\Users\username>python insert.py
array('i', [1, 2, 6, 3, 4, 5])

In the above example, we create an array of five elements and insert a new element at the second position. The new element is added at the second position and the remaining elements are shifted to the right.

remove() Method

The remove()remove() method removes the first occurrence of the element with the specified value. The syntax of the remove()remove() method is:

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

Output:

command
C:\Users\username>python remove.py
array('i', [1, 2, 3, 5])
command
C:\Users\username>python remove.py
array('i', [1, 2, 3, 5])

In the above example, we create an array of five elements and remove the element with the value 4. The first occurrence of the element with the value 4 is removed from the array.

pop() Method

The pop()pop() method removes the element at the specified position. The syntax of the pop()pop() method is:

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

Output:

command
C:\Users\username>python pop.py
array('i', [1, 2, 4, 5])
command
C:\Users\username>python pop.py
array('i', [1, 2, 4, 5])

In the above example, we create an array of five elements and remove the element at the second position. The element at the second position is removed from the array.

clear() Method

The clear()clear() method removes all the elements from the array. The syntax of the clear()clear() method is:

clear.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.clear()
 
print(my_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 clear.py
array('i')
command
C:\Users\username>python clear.py
array('i')

In the above example, we create an array of five elements and remove all the elements from the array. The array becomes empty after removing all the elements.

index() Method

The index()index() method returns the index of the first occurrence of the element with the specified value. The syntax of the index()index() method is:

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

Output:

command
C:\Users\username>python index.py
1
command
C:\Users\username>python index.py
1

In the above example, we create an array of six elements and find the index of the first occurrence of the element with the value 2. The index of the first occurrence of the element with the value 2 is 1.

count() Method

The count()count() method returns the number of elements with the specified value. The syntax of the count()count() method is:

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

Output:

command
C:\Users\username>python count.py
2
command
C:\Users\username>python count.py
2

In the above example, we create an array of six elements and count the number of elements with the value 2. The number of elements with the value 2 is 2.

sort() Method

The sort()sort() method sorts the array. The syntax of the sort()sort() method is:

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

Output:

command
C:\Users\username>python sort.py
array('i', [1, 2, 3, 4, 5])
command
C:\Users\username>python sort.py
array('i', [1, 2, 3, 4, 5])

In the above example, we create an array of five elements and sort the array. The elements of the array are sorted in ascending order.

reverse() Method

The reverse()reverse() method reverses the order of the array. The syntax of the reverse()reverse() method is:

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

Output:

command
C:\Users\username>python reverse.py
array('i', [5, 4, 3, 2, 1])
command
C:\Users\username>python reverse.py
array('i', [5, 4, 3, 2, 1])

In the above example, we create an array of five elements and reverse the order of the array. The elements of the array are reversed.

copy() Method

The copy()copy() method returns a copy of the array. The syntax of the copy()copy() method is:

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

Output:

command
C:\Users\username>python copy.py
array('i', [1, 2, 3, 4, 5])
command
C:\Users\username>python copy.py
array('i', [1, 2, 3, 4, 5])

In the above example, we create an array of five elements and copy the array to a new array. The new array is a copy of the original array.

extend() Method

The extend()extend() method adds the elements of an iterable to the end of the array. The syntax of the extend()extend() method is:

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

In the above example, we create an array of five elements and extend the array with three new elements. The three new elements are added at the end of the array.

fromlist() Method

The fromlist()fromlist() method appends the contents of the list to the array. The syntax of the fromlist()fromlist() method is:

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

Output:

command
C:\Users\username>python fromlist.py
array('i', [1, 2, 3, 4, 5, 6, 7, 8])
command
C:\Users\username>python fromlist.py
array('i', [1, 2, 3, 4, 5, 6, 7, 8])

In the above example, we create an array of five elements and append the contents of the list to the array. The contents of the list are added at the end of the array.

tolist() Method

The tolist()tolist() method converts the array to an ordinary list with the same items. The syntax of the tolist()tolist() method is:

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

Output:

command
C:\Users\username>python tolist.py
[1, 2, 3, 4, 5]
command
C:\Users\username>python tolist.py
[1, 2, 3, 4, 5]

In the above example, we create an array of five elements and convert the array to a list. The array is converted to a list with the same items.

tofile() Method

The tofile()tofile() method writes all the array elements to a file. The syntax of the tofile()tofile() method is:

tofile.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
 
f = open('my_array.txt', 'w')
my_array.tofile(f)
f.close()
tofile.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
 
f = open('my_array.txt', 'w')
my_array.tofile(f)
f.close()

Output:

command
C:\Users\username>python tofile.py
command
C:\Users\username>python tofile.py
my_array.txt
1 2 3 4 5
my_array.txt
1 2 3 4 5

In the above example, we create an array of five elements and write the array elements to a file. The array elements are written to the file in the same order.

typecode() Method

The typecode()typecode() method returns the type code of the array. The syntax of the typecode()typecode() method is:

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

Output:

command
C:\Users\username>python typecode.py
i
command
C:\Users\username>python typecode.py
i

In the above example, we create an array of five elements and print the type code of the array. The type code of the array is ii.

itemsize() Method

The itemsize()itemsize() method returns the size of each element of the array in bytes. The syntax of the itemsize()itemsize() method is:

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

Output:

command
C:\Users\username>python itemsize.py
4
command
C:\Users\username>python itemsize.py
4

In the above example, we create an array of five elements and print the size of each element of the array in bytes. The size of each element of the array is 4 bytes.

buffer_info() Method

The buffer_info()buffer_info() method returns a tuple containing the address in which array is stored and the number of elements in it. The syntax of the buffer_info()buffer_info() method is:

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

Output:

command
C:\Users\username>python buffer_info.py
(140714816, 5)
command
C:\Users\username>python buffer_info.py
(140714816, 5)

In the above example, we create an array of five elements and print the address in which the array is stored and the number of elements in it. The address in which the array is stored is 140714816 and the number of elements in the array is 5.

byteswap() Method

The byteswap()byteswap() method reverses the order of the bytes in the array. The syntax of the byteswap()byteswap() method is:

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

Output:

command
C:\Users\username>python byteswap.py
array('i', [16777216, 33554432, 50331648, 67108864, 83886080])
command
C:\Users\username>python byteswap.py
array('i', [16777216, 33554432, 50331648, 67108864, 83886080])

In the above example, we create an array of five elements and reverse the order of the bytes in the array. The order of the bytes in the array is reversed.

frombytes() Method

The frombytes()frombytes() method appends bytes from the string to the array. The syntax of the frombytes()frombytes() method is:

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

Output:

command
C:\Users\username>python frombytes.py
array('i', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
command
C:\Users\username>python frombytes.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 append bytes from the string to the array. The bytes from the string are appended to the array.

fromfile() Method

The fromfile()fromfile() method reads the array from the file, according to the dtype. The syntax of the fromfile()fromfile() method is:

fromfile.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
 
f = open('my_array.txt', 'r')
my_array.fromfile(f, 5)
f.close()
 
print(my_array)
fromfile.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
 
f = open('my_array.txt', 'r')
my_array.fromfile(f, 5)
f.close()
 
print(my_array)

Output:

command
C:\Users\username>python fromfile.py
array('i', [1, 2, 3, 4, 5])
command
C:\Users\username>python fromfile.py
array('i', [1, 2, 3, 4, 5])
my_array.txt
1 2 3 4 5
my_array.txt
1 2 3 4 5

In the above example, we create an array of five elements and read the array from the file. The array is read from the file and stored in the array.

fromstring() Method

The fromstring()fromstring() method appends items from the string into the array. The syntax of the fromstring()fromstring() method is:

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

Output:

command
C:\Users\username>python fromstring.py
array('i', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
command
C:\Users\username>python fromstring.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 append items from the string into the array. The items from the string are appended to the array.

fromunicode() Method

The fromunicode()fromunicode() method appends items from the unicode string into the array. The syntax of the fromunicode()fromunicode() method is:

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

Output:

command
C:\Users\username>python fromunicode.py
array('i', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
command
C:\Users\username>python fromunicode.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 append items from the unicode string into the array. The items from the unicode string are appended to the array.

tobytes() Method

The tobytes()tobytes() method converts the array to an array of machine values and returns the bytes representation. The syntax of the tobytes()tobytes() method is:

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

Output:

command
C:\Users\username>python tobytes.py
b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00'
command
C:\Users\username>python tobytes.py
b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00'

In the above example, we create an array of five elements and convert the array to an array of machine values and returns the bytes representation. The array is converted to an array of machine values and returns the bytes representation.

Conclusion

In this tutorial, we have learned how to use the array methods in Python with the help of examples. The array methods are the built-in methods in Python. These methods are used to perform different operations on the array. For more information, visit the official documentation of the array methods. For more tutorials, visit our Python Central Hub.

Was this page helpful?

Let us know how we did