As you've learned in the previous lessons, the most common Python data structures might not always be your best choice. Python lists are flexible, extremely useful, and a great choice for many everyday tasks. However, sometimes you need something that is a little more specialized. Python's standard library comes equipped with many well-implemented idiomatic data types to fit your specific needs.
In this lesson, you'll learn about another one of these: array.array.
How to Create a Typed Python Array
The array module in Python's standard library provides a mutable sequence data type, array, that is similar to a list but with the following differences:
- Typed:
array.arraystores items of a single type specified at the time of creation. This can be useful if you need to store a large number of items and want to optimize memory usage by using the smallest possible data type. - Contiguous Memory:
array.arraystores items in a contiguous block of memory, which can make it more efficient than a list in terms of memory consumption and performance for certain operations.
If the data you want to store is of a single type and you want to optimize for memory usage, then array.array could be a better pick than a Python list.
Use Array Method
Import the built-in array module that comes packaged in Python's standard library:
import array
The array module, and specifically using array.array(), provides a quick way to create and manipulate arrays in Python.
Create an array with a specific type and size:
a = array.array('i', [0, 0, 0, 0, 0])
This creates an array of integers (type code 'i') with five elements, all initialized to 0. The type code specifies the type of elements that the array can hold, for example:
'i'for integers'f'for floating point numbers'u'for Unicode characters
You can create a number of other types of typed arrays. Check out the official documentation on array for a list of all of them.
Python's typed arrays also come with several methods that you can use to manipulate the array. These methods will look familiar when you think of Python lists.
How to Use a Typed Array
You can access and modify elements in the array using indexing, just like with a Python list:
# access the element at index 2
print(a[2]) # 0
# modify the element at index 2
a[2] = 10
print(a[2]) # 10
Append Elements
You can append elements to the end of the array:
a.append(20)
print(a) # array('i', [0, 0, 10, 0, 0, 20])
The .append() method adds a new element to the end of the array.
Insert Elements
If you need to add an element in a different position, then you can also insert elements at a specific index:
a.insert(3, 30)
print(a) # array('i', [0, 0, 10, 30, 0, 0, 20])
The .insert() method inserts a new element at a specific index in the array.
Remove Elements
Python's array.array even comes with a familiar .pop() method that allows you to remove elements from the array:
import array
# Create an array of integers
a = array.array('i', [10, 20, 30, 40, 50])
# Remove and return the last element from the array
last = a.pop()
print(last) # 50
print(a) # array('i', [10, 20, 30, 40])
# Remove and return the element at index 2
middle = a.pop(2)
print(middle) # 30
print(a) # array('i', [10, 20, 40])
Using array.array will feel familiar and similar to using a Python list. However, by swapping your lists for such typed arrays, you can potentially gain some performance improvements in your code.
When to Use a Typed Array
It might not always be straightforward to decide between using a list or an array and it depends on your use case. However, here are a few examples that might help you decide when you might want to use array.array over a Python list:
Same Type
You have a large number of items that are all of the same types, and you want to optimize memory usage by using the smallest possible data type. For example, if you have a list of integers that are all between 0 and 255, you can use an array.array of type 'b' (signed char) to store them, which will only use a single byte per item.
Many Operations
You need to perform a lot of operations on a sequence that involves appending, inserting, or deleting items, and you want to do so as efficiently as possible. Because array.array stores items in a contiguous block of memory, these operations can be faster than with a list, which requires reallocating memory and moving items around whenever the list grows or shrinks.
Memory Efficiency
You want to use a sequence that is more memory-efficient than a list, and you don't need all the features of a list (indexing, slicing, etc.).
That being said, array.array is not always the best choice for storing data in Python. Lists are generally more flexible and have a larger set of built-in methods and functions, so they may be more convenient to use in many cases.
In addition, array.array is not as fast as other specialized data structures like numpy.array or collections.deque, which may be more suitable for some code you'll write.
However, you should consider whether a Python list or a more specialized data structure like numpy.array or collections.deque would be more appropriate for your needs.
Additional Resources
- Explore CodingNomads Data Structures and Algorithms course
- Python documentation on
array - Real Python Array Data Structure
Summary: Python Array with Type
- The
arraymodule comes with thearray()method to create a typed Python array - This typed array holds a continuous block of memory in your machine
- Typed array is great if you need to store a large number of items of the same type and you want to optimize the memory usage and performance of operations like appending, inserting, and deleting items
When to Use Typed Arrays
- Elements of the same type
- Many operations
- Improve memory efficiency