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. | Method | Description | Example |
---|---|---|---|
1 | append() | Adds an element at the end of the array | arr.append(5) arr.append(5) |
2 | insert() | Adds an element at the specified position | arr.insert(2, 5) arr.insert(2, 5) |
3 | remove() | Removes the first occurrence of the element with the specified value | arr.remove(5) arr.remove(5) |
4 | pop() | Removes the element at the specified position | arr.pop(2) arr.pop(2) |
5 | clear() | Removes all the elements from the array | arr.clear() arr.clear() |
6 | index() | Returns the index of the first element with the specified value | arr.index(5) arr.index(5) |
7 | count() | Returns the number of elements with the specified value | arr.count(5) arr.count(5) |
8 | sort() | Sorts the array | arr.sort() arr.sort() |
9 | reverse() | Reverses the order of the array | arr.reverse() arr.reverse() |
10 | copy() | Returns a copy of the array | arr.copy() arr.copy() |
11 | extend() | Adds the elements of an array to the end of the current array | arr.extend(arr2) arr.extend(arr2) |
12 | fromlist() | Appends the contents of the list to the array | arr.fromlist(list) arr.fromlist(list) |
13 | tolist() | Converts the array to an ordinary list with the same items | arr.tolist() arr.tolist() |
14 | tofile() | Writes all the array elements to a file | arr.tofile(f) arr.tofile(f) |
15 | typecode() | Returns the type code of the array | arr.typecode arr.typecode |
16 | itemsize() | Returns the size of each element of the array in bytes | arr.itemsize arr.itemsize |
17 | buffer_info() | Returns a tuple containing the address in which array is stored and the number of elements in it | arr.buffer_info() arr.buffer_info() |
18 | byteswap() | Reverses the order of the bytes in the array | arr.byteswap() arr.byteswap() |
19 | frombytes() | Appends bytes from the string to the array | arr.frombytes(s) arr.frombytes(s) |
20 | fromfile() | Reads the array from the file, according to the dtype | arr.fromfile(f, n) arr.fromfile(f, n) |
21 | fromstring() | Appends items from the string into the array | arr.fromstring(s) arr.fromstring(s) |
22 | fromunicode() | Appends items from the unicode string into the array | arr.fromunicode(s) arr.fromunicode(s) |
23 | tobytes() | Converts the array to an array of machine values and returns the bytes representation | arr.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:
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 append.py
array('i', [1, 2, 3, 4, 5, 6])
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.insert(2, 6)
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.insert(2, 6)
print(my_array)
Output:
C:\Users\username>python insert.py
array('i', [1, 2, 6, 3, 4, 5])
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.remove(4)
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.remove(4)
print(my_array)
Output:
C:\Users\username>python remove.py
array('i', [1, 2, 3, 5])
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.pop(2)
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.pop(2)
print(my_array)
Output:
C:\Users\username>python pop.py
array('i', [1, 2, 4, 5])
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:
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 clear.py
array('i')
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 2, 4, 5])
print(my_array.index(2))
import array as arr
my_array = arr.array('i', [1, 2, 3, 2, 4, 5])
print(my_array.index(2))
Output:
C:\Users\username>python index.py
1
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 2, 4, 5])
print(my_array.count(2))
import array as arr
my_array = arr.array('i', [1, 2, 3, 2, 4, 5])
print(my_array.count(2))
Output:
C:\Users\username>python count.py
2
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:
import array as arr
my_array = arr.array('i', [3, 2, 5, 4, 1])
my_array.sort()
print(my_array)
import array as arr
my_array = arr.array('i', [3, 2, 5, 4, 1])
my_array.sort()
print(my_array)
Output:
C:\Users\username>python sort.py
array('i', [1, 2, 3, 4, 5])
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.reverse()
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.reverse()
print(my_array)
Output:
C:\Users\username>python reverse.py
array('i', [5, 4, 3, 2, 1])
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
new_array = my_array.copy()
print(new_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
new_array = my_array.copy()
print(new_array)
Output:
C:\Users\username>python copy.py
array('i', [1, 2, 3, 4, 5])
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:
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 extend.py
array('i', [1, 2, 3, 4, 5, 6, 7, 8])
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:
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)
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:
C:\Users\username>python fromlist.py
array('i', [1, 2, 3, 4, 5, 6, 7, 8])
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array.tolist())
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array.tolist())
Output:
C:\Users\username>python tolist.py
[1, 2, 3, 4, 5]
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:
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()
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:
C:\Users\username>python tofile.py
C:\Users\username>python tofile.py
1 2 3 4 5
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array.typecode)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array.typecode)
Output:
C:\Users\username>python typecode.py
i
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 i
i
.
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array.itemsize)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array.itemsize)
Output:
C:\Users\username>python itemsize.py
4
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array.buffer_info())
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array.buffer_info())
Output:
C:\Users\username>python buffer_info.py
(140714816, 5)
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.byteswap()
print(my_array)
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
my_array.byteswap()
print(my_array)
Output:
C:\Users\username>python byteswap.py
array('i', [16777216, 33554432, 50331648, 67108864, 83886080])
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:
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)
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:
C:\Users\username>python frombytes.py
array('i', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
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:
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)
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:
C:\Users\username>python fromfile.py
array('i', [1, 2, 3, 4, 5])
C:\Users\username>python fromfile.py
array('i', [1, 2, 3, 4, 5])
1 2 3 4 5
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:
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)
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:
C:\Users\username>python fromstring.py
array('i', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
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:
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)
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:
C:\Users\username>python fromunicode.py
array('i', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
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:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array.tobytes())
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array.tobytes())
Output:
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'
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