Python Arrays

In python, arrays are useful for storing multiple values of the same data type, but we don’t have direct built-in support for arrays in python. To work with arrays in python, you need to import the NumPy library or use lists in python to achieve the array's functionality.

 

This article will show you how to use python lists as arrays to store the multiple values in a single variable with examples.

Create Arrays

In python, you can create the array by enclosing the items within square brackets [ ] and the items must be separated by commas.

 

Following is the example of creating the arrays in python.

 

# Array with integer types
a = [30, 10, 20]
# Array with string types
b = ["Suresh", "Rohini", "Trishi", "Hanshith"]
# Empty Array
c = []
print("a = ", a)
print("b = ", b)
print("c = ", c)

If you observe the above example, we created different arrays and printing the array items using the print method in python.

 

When you execute the above python program, you will get the result as shown below.

 

a = [30, 10, 20]
b = ['Suresh', 'Rohini', 'Trishi', 'Hanshith']
d = []

Access Elements from Array

In python, you can access elements from an array by using index values. In array, the index values will always start from 0. If your array has 4 elements, you can access the array elements by using index ranges 0 to 3.

 

Following is the example of accessing the elements from an array in python.

 

ary = [10, 20, 30, 40]
print(ary[0])
print(ary[2])
print(ary[3])

If you observe the above example, we are accessing the array elements using different index values with square brackets [ ].

 

When you execute the above python example, you will get the result as shown below.

 

10
30
Tutlane

Change Array Item Value

Using index values, you can change or update the value of the required item in the array.

 

Following is the example of changing the particular array item value using index number in python.

 

ary = [10, 20, 30, 40]
print("Before: ",ary)
ary[1] = 5
ary[3] = 50
print("After: ",ary)

When you execute the above python array example, you will get the result like as shown below.

 

Before: [10, 20, 30, 40]
After: [10, 5, 30, 50]

Add or Append Items to Array

In python, the append() method is useful to add or append items at the end of the array.

 

Following is the example of adding items to the array using the append() method in python.

 

ary = [10, 20, 30]
ary.append(40)
ary.append(50)
print(ary)

The above python array example will return the result like as shown below.

 

[10, 20, 30, 40, 50]

Remove Elements from Array

In python, you can delete/remove items from an array using either. remove(), pop(), or clear() methods based on our requirements.

 

In python, if you want to remove array items based on the value, you can use the remove() method.

 

Following is the example of removing the array items based on the value using the remove() method.

 

ary = [10, 20, 30, 40, 50]
print("Before: ", ary)
ary.remove(20)
ary.remove(40)
print("After: ", ary)

The above python array example will return the result like as shown below.

 

Before: [10, 20, 30, 40, 50]
After: [10, 30, 50]

In python, the pop() method helps remove the array items based on the specified index position. In case if index position is not specified, by default, it will remove the last item.

 

Following is the example of using the pop() method to remove array items.

 

ary = [10, 20, 30, 40, 50]
print("Before: ", ary)
ary.pop(2)
ary.pop()
print("After: ", ary)

When you execute the above python array program, you will get the result like as shown below.

 

Before: [10, 20, 30, 40, 50]
After: [10, 20, 40]

If you want to clear or empty array items, you can use the clear() method.

 

Following is the example of removing all array elements using the clear() method in python.

 

ary = [10, 20, 30, 40, 50]
print("Before: ", ary)
ary.clear()
print("After: ", ary)

The above python array example will return the result like as shown below.

 

Before: [10, 20, 30, 40, 50]
After: []

Array Length

In python, to count the number of items in the array, use the len() function.

 

Following is the example of using the len() function in python to get the array length/size.

 

ary = [10, 20, 30, 40, 50]
print("Array Size: ", len(ary)) #List Size: 5

By using len() function, you can also check whether the array is empty or not like as shown below. 

 

ary = []
count = len(ary)
if count > 0:
    print("Array size: ", count)
else:
    print("Array is empty")

The above array example will return the result like as shown below.

 

Array is empty

Join or Concatenate Arrays

In python, the + operator is useful to join or concatenate multiple arrays and return a new array with all array elements.

 

Following is the example of joining the arrays using the + operator in python.

 

ary1 = [10, 20, 30]
ary2 = [40, 50]
ary3 = ary1 + ary2
print(ary3)

The above array example will return the result like as shown below.

 

[10, 20, 3, 40, 50]

Check If Item Exists in Array

By using in and not in operators, you can check whether the particular item exists in the array or not.

 

Following is the example to verify whether the particular exists in the array or not using in operator in python.

 

ary = [10, 20, 30, 40, 50]
print("20 in Array: ", 20 in ary)
print("50 in Array: ", 30  in ary)
print("100 in Array: ", 100 in ary)

The above array example will return the result like as shown below.

 

20 in Array: True
50 in Array: True
100 in Array: False

Loop through Array Elements

By using for loop, you can loop through the elements of the array based on your requirements.

 

Following is the example of looping through the array items using for loop in python.

 

ary = [10, 20, 30, 40, 50]
for item in ary:
    print(item)

The above array example will return the result as shown below.

 

10
20
30
40
50

Python Sort Array Elements

In python, the sort() method is useful to sort the array elements. By default, the sort() method will sort the array elements in ascending order.

 

Following is the example of sorting the array elements in ascending order using the sort() method in python.

 

ary = ["tutlane", "learn", "python"]
print("Before: ", ary)
ary.sort()
print("After: ",ary)

The above array example will return the result like as shown below.

 

Before: ['tutlane', 'learn', 'python']
After: ['learn', 'python', 'tutlane']

To sort the array elements in descending order, you need to use the optional reverse = True property with the sort() method as shown below. 

 

ary = ["learn", "python", "tutlane"]
print("Before: ", ary)
ary.sort(reverse = True)
print("After: ",ary)

The above array example will return the result like as shown below.

 

Before: ['learn', 'python', 'tutlane']
After: ['tutlane', 'python', 'learn']

To sort array items either in ascending or descending order, you need to make sure that all the array items must be the same type otherwise, you will get a TypeError exception.

 

ary = [30, 20, 10, "tutlane", "learn"]
print("Before: ", ary)
ary.sort()
print("After: ",ary)

When you execute the above python array example, you will get the result like as shown below.

 

Traceback (most recent call last):
  File "pythonarrays.py", line 3, in <module>
    ary.sort()
TypeError: '<' not supported between instances of 'str' and 'int'

Python Array Methods

In python, the list/array object has a set of built-in methods to perform different operations based on your requirements.

 

Following are the built-in methods that you can use on lists/arrays in python.

 

MethodDescription
append() It is useful to add elements at the end of the list.
insert() It is useful to add elements at the specified position.
extend() It is useful to add elements of one list to the end of another list.
pop() It is useful to remove the element at the specified position.
remove() It is useful to remove the element with the specified value.
clear() It is useful to remove all the elements of the list.
index() It will return the index of the first matched element.
count() It will return the number of elements with the specified value.
sort() It is useful to sort (ascending/descending) list items.
reverse() It is useful to reverse the order of list items.
copy() It is useful to return a copy of the list.