Skip to content

Python Arrays

In this article, we will explore the features and capabilities of Python arrays. We will learn how to create, manipulate, and work with them effectively in your code.

What are Arrays?

Arrays are a powerful data structure in programming that allow you to store and manipulate a collection of elements of the same type. In Python, arrays are created using the array module, which provides a simple interface to create, manipulate, and work with arrays.

Creating Arrays

To create an array in Python, we first need to import the array module. Then we can create an array by specifying the type of elements we want to store, and the values of those elements.

Creating arrays in Python

python
import array as arr

# create an array of integers
my_array = arr.array('i', [1, 2, 3, 4, 5])

# create an array of floats
my_float_array = arr.array('f', [1.0, 2.0, 3.0, 4.0, 5.0])

# create an array of characters
my_char_array = arr.array('u', ['a', 'b', 'c', 'd', 'e'])

Accessing Array Elements

Once we have created an array, we can access its elements using their index. Array indices start from 0, so the first element in the array has an index of 0, the second element has an index of 1, and so on.

Access array elements in Python

python
import array as arr

# create an array of integers
my_array = arr.array('i', [1, 2, 3, 4, 5])

# access the first element of the array
print(my_array[0]) # Output: 1

# access the second element of the array
print(my_array[1]) # Output: 2

# access the last element of the array
print(my_array[-1]) # Output: 5

Array Slicing

We can also access a range of elements in an array using slicing. Slicing allows us to create a new array that contains a subset of the elements from the original array.

Slice arrays in Python

python
import array as arr

# create an array of integers
my_array = arr.array('i', [1, 2, 3, 4, 5])

# slice the array to get the first three elements
print(my_array[0:3]) # Output: array('i', [1, 2, 3])

# slice the array to get the last three elements
print(my_array[-3:]) # Output: array('i', [3, 4, 5])

Manipulating Arrays

Arrays in Python are mutable, which means we can modify their elements after they have been created. We can add or remove elements, change the value of an element, or convert the array to a different type.

Adding Elements

To add an element to an array, we can use the append() method. This method adds the element to the end of the array.

Adding an element to a Python array

python
import array as arr

# create an array of integers
my_array = arr.array('i', [1, 2, 3, 4, 5])

# add a new element to the array
my_array.append(6)

# print the updated array
print(my_array) # Output: array('i', [1, 2, 3, 4, 5, 6])

Removing Elements

To remove an element from an array, we can use the remove() method. This method removes the first occurrence of the specified element from the array.

Remove an element from a Python array

python
import array as arr

# create an array of integers
my_array = arr.array('i', [1, 2, 3, 4, 5])

# remove an element from the array
my_array.remove(3)

# print the updated array
print(my_array) # Output: array('i', [1, 2, 4, 5])

Changing Elements

To change the value of an element in an array, we can simply assign a new value to the element at the specified index.

Update array elements in Python

python
import array as arr

# create an array of integers
my_array = arr.array('i', [1, 2, 3, 4, 5])

# change the value of an element in the array
my_array[2] = 10

# print the updated array
print(my_array) # Output: array('i', [1, 2, 10, 4, 5])

Changing Element Type

To change the type of elements in an array, you must create a new array with the desired typecode, passing the original array as the initializer. Note that the typecode attribute is read-only and cannot be assigned directly.

Set a new typecode for an array in Python

python
import array as arr

# create an array of integers
my_array = arr.array('i', [1, 2, 3, 4, 5])

# change the type of elements in the array to floats
my_array = arr.array('f', my_array)

# print the updated array
print(my_array) # Output: array('f', [1.0, 2.0, 3.0, 4.0, 5.0])

Other Common Methods

The array module provides several other useful methods for managing elements:

  • extend(iterable): Adds multiple elements from an iterable to the end of the array.
  • insert(index, element): Inserts an element at a specified index.
  • pop([index]): Removes and returns the element at the given index (defaults to the last element).
  • tolist(): Converts the array to a standard Python list.

Conclusion

In this article, we have covered the basics of Python arrays and how to use them in your code. We have learned how to create, access, and manipulate arrays. Note that while the array module is useful for memory-efficient storage of homogeneous numeric data, Python lists are generally preferred for everyday use due to their flexibility and broader method support. With this knowledge, you should be able to use arrays effectively in your programs, and take advantage of their powerful capabilities.

Practice

What are some methods/actions applied to Python arrays?

Dual-run preview — compare with live Symfony routes.