Skip to content

Array Operations

Array Operations

In this tutorial, you will learn how to perform operations on arrays. how to perform loop, copy, reverse, sort, search, and join operations on arrays.

Looping Array Elements

You can access the array elements by using the loop statement.

For Loop

The for loop is used to iterate over the array elements. The for loop is used to iterate over the array elements.

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

Output:

command
C:\Users\username>python array_for_loop.py
1
2
3
4
5
command
C:\Users\username>python array_for_loop.py
1
2
3
4
5

In the above example, we create an array of five elements and iterate over the array elements using the for loop.

While Loop

The while loop is used to iterate over the array elements until the given condition is true.

array_while_loop.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
 
i = 0
while i < len(my_array):
    print(my_array[i])
    i += 1
array_while_loop.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
 
i = 0
while i < len(my_array):
    print(my_array[i])
    i += 1

Output:

command
C:\Users\username>python array_while_loop.py
1
2
3
4
5
command
C:\Users\username>python array_while_loop.py
1
2
3
4
5

In the above example, we create an array of five elements and iterate over the array elements using the while loop.

Using Range() Function

The range() function returns a sequence of numbers starting from zero to the specified number minus one.

array_range.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
 
for i in range(len(my_array)):
    print(my_array[i])
array_range.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
 
for i in range(len(my_array)):
    print(my_array[i])

Output:

command
C:\Users\username>python array_range.py
1
2
3
4
5
command
C:\Users\username>python array_range.py
1
2
3
4
5

In the above example, we create an array of five elements and iterate over the array elements using the range() function.

Copying Array

You can copy the array elements to another array using the copy()copy() method and deepcopy()deepcopy() function.

= operator

The == operator is used to copy the array elements to another array.

array_copy.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
new_array = my_array
 
print("Before updating")
print(my_array)
print(new_array)
 
new_array[0] = 6
 
print("After updating")
print(my_array)
print(new_array)
array_copy.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
new_array = my_array
 
print("Before updating")
print(my_array)
print(new_array)
 
new_array[0] = 6
 
print("After updating")
print(my_array)
print(new_array)

Output:

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

In the above example, we create an array of five elements and copy the array elements to another array using the == operator. After updating, we change the first element of the new array. The first element of the original array is also changed.

copy() method

The copy()copy() method returns a shallow copy of the array.

array_copy.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
new_array = my_array.copy()
 
print("Before updating")
print(my_array)
print(new_array)
 
new_array[0] = 6
 
print("After updating")
print(my_array)
print(new_array)
array_copy.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
new_array = my_array.copy()
 
print("Before updating")
print(my_array)
print(new_array)
 
new_array[0] = 6
 
print("After updating")
print(my_array)
print(new_array)

Output:

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

In the above example, we create an array of five elements and copy the array elements to another array using the copy()copy() method. After updating, we change the first element of the new array. The first element of the original array is not changed.

deepcopy() function

The deepcopy()deepcopy() function returns a deep copy of the array.

array_deepcopy.py
import array as arr
import copy
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
new_array = copy.deepcopy(my_array)
 
print("Before updating")
print(my_array)
print(new_array)
 
new_array[0] = 6
 
print("After updating")
print(my_array)
print(new_array)
array_deepcopy.py
import array as arr
import copy
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
new_array = copy.deepcopy(my_array)
 
print("Before updating")
print(my_array)
print(new_array)
 
new_array[0] = 6
 
print("After updating")
print(my_array)
print(new_array)

Output:

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

In the above example, we create an array of five elements and copy the array elements to another array using the deepcopy()deepcopy() function. After updating, we change the first element of the new array. The first element of the original array is not changed.

Reversing Array

Reverse the order of the items in the array using the reverse()reverse() method and negative indexing.

Negative Indexing

Negative indexing is used to access the array elements from the end of the array.

array_reverse.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
reverse_array = my_array[::-1]
 
print("Original array")
print(my_array)
print("Reversed array")
print(reverse_array)
array_reverse.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
reverse_array = my_array[::-1]
 
print("Original array")
print(my_array)
print("Reversed array")
print(reverse_array)

Output:

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

In the above example, we create an array of five elements and reverse the array elements using negative indexing.

insert() method

The insert()insert() method is used to insert the array elements at a given index.

array_reverse.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
reverse_array = arr.array('i')
 
for i in my_array:
    reverse_array.insert(0, i)
 
print("Original array")
print(my_array)
print("Reversed array")
print(reverse_array)
array_reverse.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
reverse_array = arr.array('i')
 
for i in my_array:
    reverse_array.insert(0, i)
 
print("Original array")
print(my_array)
print("Reversed array")
print(reverse_array)

Output:

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

In the above example, we create an array of five elements and reverse the array elements using the insert()insert() method.

reverse() method

The reverse()reverse() method is used to reverse the order of the items in the array.

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

Output:

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

In the above example, we create an array of five elements and reverse the array elements using the reverse()reverse() method.

Sorting Array

Sort the array elements in ascending or descending order using the sort()sort() method.

Using Sorting Algorithm (Bubble Sort)

Bubble sort algorithm is used to sort the array elements in ascending order.

array_sort.py
import array as arr
 
my_array = arr.array('i', [5, 2, 3, 4, 1])
 
for i in range(len(my_array)):
    for j in range(i + 1, len(my_array)):
        if my_array[i] > my_array[j]:
            temp = my_array[i]
            my_array[i] = my_array[j]
            my_array[j] = temp
 
print(my_array)
array_sort.py
import array as arr
 
my_array = arr.array('i', [5, 2, 3, 4, 1])
 
for i in range(len(my_array)):
    for j in range(i + 1, len(my_array)):
        if my_array[i] > my_array[j]:
            temp = my_array[i]
            my_array[i] = my_array[j]
            my_array[j] = temp
 
print(my_array)

Output:

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

In the above example, we create an array of five elements and sort the array elements in ascending order using the bubble sort algorithm.

Using Sorting Algorithm (Selection Sort)

Selection sort algorithm is used to sort the array elements in ascending order.

array_sort.py
import array as arr
 
my_array = arr.array('i', [5, 2, 3, 4, 1])
 
for i in range(len(my_array)):
    min_index = i
    for j in range(i + 1, len(my_array)):
        if my_array[min_index] > my_array[j]:
            min_index = j
 
    temp = my_array[i]
    my_array[i] = my_array[min_index]
    my_array[min_index] = temp
 
print(my_array)
array_sort.py
import array as arr
 
my_array = arr.array('i', [5, 2, 3, 4, 1])
 
for i in range(len(my_array)):
    min_index = i
    for j in range(i + 1, len(my_array)):
        if my_array[min_index] > my_array[j]:
            min_index = j
 
    temp = my_array[i]
    my_array[i] = my_array[min_index]
    my_array[min_index] = temp
 
print(my_array)

Output:

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

In the above example, we create an array of five elements and sort the array elements in ascending order using the selection sort algorithm.

sort() method

The sort()sort() method is used to sort the array elements in ascending order.

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

Output:

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

In the above example, we create an array of five elements and sort the array elements in ascending order using the sort()sort() method.

Using Sorted() Function

The sorted()sorted() function is used to sort the array elements in ascending order.

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

Output:

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

In the above example, we create an array of five elements and sort the array elements in ascending order using the sorted()sorted() function.

Searching Array

Search the array elements using the index()index() method and inin operator.

index() method

The index()index() method returns the index of the specified element in the array.

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

Output:

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

In the above example, we create an array of five elements and search the array elements using the index()index() method.

in operator

The inin operator returns TrueTrue if the specified element is present in the array.

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

Output:

command
C:\Users\username>python array_search.py
True
command
C:\Users\username>python array_search.py
True

In the above example, we create an array of five elements and search the array elements using the inin operator.

Joining Array

Join the array elements using the ++ operator and extend()extend() method.

+ operator

The ++ operator is used to join the array elements.

array_join.py
import array as arr
 
my_array1 = arr.array('i', [1, 2, 3, 4, 5])
my_array2 = arr.array('i', [6, 7, 8, 9, 10])
 
new_array = my_array1 + my_array2
 
print(new_array)
array_join.py
import array as arr
 
my_array1 = arr.array('i', [1, 2, 3, 4, 5])
my_array2 = arr.array('i', [6, 7, 8, 9, 10])
 
new_array = my_array1 + my_array2
 
print(new_array)

Output:

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

In the above example, we create two arrays of five elements and join the array elements using the ++ operator.

extend() method

The extend()extend() method is used to join the array elements.

array_join.py
import array as arr
 
my_array1 = arr.array('i', [1, 2, 3, 4, 5])
my_array2 = arr.array('i', [6, 7, 8, 9, 10])
 
my_array1.extend(my_array2)
 
print(my_array1)
array_join.py
import array as arr
 
my_array1 = arr.array('i', [1, 2, 3, 4, 5])
my_array2 = arr.array('i', [6, 7, 8, 9, 10])
 
my_array1.extend(my_array2)
 
print(my_array1)

Output:

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

In the above example, we create two arrays of five elements and join the array elements using the extend()extend() method.

Using fromlist() method

The fromlist()fromlist() method is used to join the array elements.

array_join.py
import array as arr
 
my_array1 = arr.array('i', [1, 2, 3, 4, 5])
my_array2 = arr.array('i', [6, 7, 8, 9, 10])
 
my_array1.fromlist(my_array2.tolist())
 
print(my_array1)
array_join.py
import array as arr
 
my_array1 = arr.array('i', [1, 2, 3, 4, 5])
my_array2 = arr.array('i', [6, 7, 8, 9, 10])
 
my_array1.fromlist(my_array2.tolist())
 
print(my_array1)

Output:

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

In the above example, we create two arrays of five elements and join the array elements using the fromlist()fromlist() method.

Using list() function

The list()list() function is used to join the array elements.

array_join.py
import array as arr
 
my_array1 = arr.array('i', [1, 2, 3, 4, 5])
my_array2 = arr.array('i', [6, 7, 8, 9, 10])
 
my_array1 = arr.array('i', list(my_array1) + list(my_array2))
 
print(my_array1)
array_join.py
import array as arr
 
my_array1 = arr.array('i', [1, 2, 3, 4, 5])
my_array2 = arr.array('i', [6, 7, 8, 9, 10])
 
my_array1 = arr.array('i', list(my_array1) + list(my_array2))
 
print(my_array1)

Output:

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

In the above example, we create two arrays of five elements and join the array elements using the list()list() function.

Conclusion

In this tutorial, you have learned how to perform operations on arrays. how to perform loop, copy, reverse, sort, search, and join operations on arrays. Now you can solve problems on arrays using Python. If you like this tutorial, please share it with others via the social media channel. Thanks for reading. For more tutorials, please visit Python Central Hub.

Was this page helpful?

Let us know how we did