Skip to content

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.

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

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)

Output:

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.

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

Output:

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.

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])

Output:

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.

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

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)

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])

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.

The 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)

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])

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

The 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)

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])

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

Reverse the order of the items in the array using the reverse() method and 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)

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])

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

The 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)

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])

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

The 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)

Output:

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() method.

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

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)

Output:

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.

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)

Output:

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.

The 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)

Output:

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() method.

The 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)

Output:

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() function.

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

The 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)

Output:

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() method.

The in operator returns True 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)

Output:

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 in operator.

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

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)

Output:

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.

The 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)

Output:

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() method.

The 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)

Output:

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() method.

The 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)

Output:

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() function.

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.


diagram an array is a container, not a vector mermaid
The operators behave the way they do on a list, because array is a sequence type. Adding two arrays concatenates them and multiplying repeats them -- there is no element-wise arithmetic anywhere in the module. That is the line where NumPy begins.
sketch Plus concatenates; it does not add p5.js
The array module gives you compact storage and nothing else. Its operators follow sequence semantics, exactly as a list does, so adding two arrays joins them end to end rather than adding them element by element. If you wanted the arithmetic, that is what NumPy is for -- and the comparison at the bottom is why people move.
pch.quizTag pch.quizDefaultTitle
  1. What does `array('i',[1,2]) + array('i',[3,4])` produce?

    pch.quizShowAnswer

    B — `array('i', [1, 2, 3, 4])` — Concatenation, exactly as with lists. `array` is a sequence type with compact storage — it adds no arithmetic semantics at all.

  2. How do you double every element of an `array`?

    pch.quizShowAnswer

    C — A comprehension, or use NumPy — `a * 2` repeats the sequence. A comprehension costs one Python-level operation per element and gives back a list; NumPy does it element-wise in C.

  3. When is `array` the right choice over NumPy?

    pch.quizShowAnswer

    B — A compact block you mostly store, or read and write to a file — and you want no dependency — It is in the standard library and has `tofile`/`fromfile` built in. The moment you want arithmetic across the block, NumPy is the better tool.

  4. What must be true to concatenate two arrays?

    pch.quizShowAnswer

    B — They must share a typecode — The result is a single block of one fixed width, so both operands must agree on the type. Mixing typecodes raises `TypeError`.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading