Post

A Quick Note on Python Arrays

In Python, the term “array” can be a bit ambiguous. While Python doesn’t have a built-in array data type in the same way as languages like C or Java, the list data type is often used as a flexible, dynamic array. For more memory-efficient storage of a single data type, Python provides the array module.

Using Lists as Arrays

For most purposes, Python’s list is a perfectly good substitute for an array. Lists are ordered, changeable, and can contain items of different data types.

1
2
3
4
5
6
7
8
9
# A list used as an array
my_array = [1, "hello", 3.14]

# Accessing elements
print(my_array[0])  # 1

# Adding elements
my_array.append(True)
print(my_array)  # [1, 'hello', 3.14, True]

The array Module

If you need to store a large number of items of the same numeric type, the array module is a more memory-efficient option than a list. You need to import the array module to use it.

1
import array

Creating an Array

When you create an array, you need to specify a type code, which determines the type of the items in the array (e.g., ‘i’ for signed integer, ‘d’ for double-precision float).

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

# An array of floats
float_array = array.array('d', [1.0, 2.5, 3.14])

Accessing and Manipulating Array Elements

Arrays behave very similarly to lists. You can access elements by index, and you can use methods like append(), insert(), and remove().

1
2
3
4
5
6
7
print(my_array[0])  # 1

my_array.append(6)
print(my_array)  # array('i', [1, 2, 3, 4, 5, 6])

my_array.remove(3)
print(my_array)  # array('i', [1, 2, 4, 5, 6])

Conclusion

While Python’s list is a versatile and powerful data structure that can be used as a general-purpose array, the array module provides a more memory-efficient solution for storing large sequences of a single numeric type. For most day-to-day programming, a list will be sufficient, but when you’re working with large numerical datasets, the array module is a valuable tool to have in your toolkit.

This post is licensed under CC BY 4.0 by the author.