Arrays in Python

  • Last updated Apr 25, 2024

In Python, an Array is a collection of similar type of data.

Array in Python can be created by importing the array module.

Syntax:

array(data type, [array list])

Example:

from array import array

nums = array("i", [1, 3, 44, 56, 77, 8])
print("int nums = ", end = "")
for i in range(len(nums)):
    print(nums[i], end = " ")
print()

nums = array("d", [1.3, 3.5, 44.46, 56.3])
print("double nums = ", end = "")
for i in range(len(nums)):
    print(nums[i], end = " ")

Output:

int nums = 1 3 44 56 77 8 
double nums = 1.3 3.5 44.46 56.3

While creating an array, the first argument you need to provide is the data type of the array values. The following table will help you create arrays of different types:

Type Code
C Type
Python Type
Minimum Size in bytes
'b'
signed char
int
1
'B'
unsigned char
int
1
'u'
Py_UNICODE
Unicode Character
2
'h'
signed short
int
2
'H'
unsigned short
int
2
'i'
signed int
int
2
'I'
unsigned int
int
4
'l'
unsigned long
int
4
'L'
unsigned long
int
4
'f'
float
int
4
'd'
double
int
8
Array Methods

Here are some useful array methods in Python:

Method
Description
append()
Adds an item at the end of an array.
clear()
Removes all items from an array.
count()
Returns the number of items in an array.
copy()
Return copy of an array.
extend()
Adds multiple items at the end of an array.
insert()
Adds an item at the specified index of an array.
pop()
Removes an item from the specified index of an array.
remove()
Removes the specified item from an array.
reverse()
Reverses the order of items in an array.
sort()
Sorts the order of items in an array.
Adding Items

In Python, you can use the append() method to add items to an array.

Example:

from array import array

# Creating an array
nums = array("i")

# Adding new values
nums.append(55)
nums.append(56)
nums.append(57)
nums.append(58)

for i in range(len(nums)):
    print(nums[i], end = " ")

Output:

55 56 57 58

In Python, you can also insert a value at a specified index using the insert() method.

Here's an example of inserting the value 55 at index 0:

from array import array

nums = array("i", [1, 2, 3, 4, 5, 6])
print("nums before = ", end = "")
for i in range(len(nums)):
    print(nums[i], end = " ")
print()

# Adding new value to a specified index
nums.insert(0,55)

print("nums after = ", end = "")
for i in range(len(nums)):
    print(nums[i], end = " ")

The output of the above code is as follows:

nums before = 1 2 3 4 5 6 
nums after = 55 1 2 3 4 5 6
Accessing Array Items

In Python, you can access values of an array by using square brackets with an index.

Example:

from array import array

nums = array("i", [1, 3, 44, 56, 77, 8])

print(nums[0])
print(nums[2])

Output:

1
44
Removing Array Items

In Python, you can remove items from an array using the remove() method.

Example:

from array import array

nums = array("i", [1, 3, 44, 56, 77, 8])
print("nums before = ", end = "")
for i in range(len(nums)):
    print(nums[i], end = " ")
print()

#remove value
nums.remove(77)
print("nums after removing = ", end = "")
for i in range(len(nums)):
    print(nums[i], end = " ")

Output:

nums before = 1 3 44 56 77 8 
nums after removing = 1 3 44 56 8

In Python, you can also remove items at a specified index using the pop() method.

Example:

from array import array

nums = array("i", [1, 3, 44, 56, 77, 8])
print("nums before = ", end = "")
for i in range(len(nums)):
    print(nums[i], end = " ")
print()

#removing value at specified index
nums.pop(2)
print("nums after = ", end = "")
for i in range(len(nums)):
    print(nums[i], end = " ")

Output:

nums before = 1 3 44 56 77 8 
nums after = 1 3 56 77 8