In Python, an array is more memory-efficient than a list. Code with arrays is more complex. Arrays can hold more elements due to their reduced memory requirements.
When using arrays, we must specify a data type with a type code. For example a lowercase "i" indicates an integer—we can create an int
array.
In this first example, please add the "from import" statement at the top of the program. This lets you access the array type more easily.
array()
is a list. The array will be made of integers equal to those specified in the list.for
-loop to enumerate, and print, the elements in the array. This is the same syntax used for a list.from array import array # Create an int array of three elements. a = array("i", [10, 20, 30]) # Display elements in array. for value in a: print(value)10 20 30
String
Suppose you have a large string
you want to store in an array. We can use the "u" type code. Here the string
"python" is transformed into an array of six Unicode characters.
tolist()
method. We then join with an empty delimiter.string
. This approach can convert an array (or list) into a usable string
.from array import array # Create a Unicode char array. a = array("u", "python") # Display letters in array. for letter in a: print(letter) # Convert array to a list. # ... Then join it. s = "".join(a.tolist()) print(s)p y t h o n python
Append
, insertMany helpful methods are available on arrays. For example, the append method adds an element at the end of the array. We can also insert, remove and count.
int
array in the first part. The second argument to the array init
method is optional.from array import array # New int array. a = array("i") # Append three integers. a.append(100) a.append(200) a.append(300) # Insert an integer at index 1. a.insert(1, 900) # Remove this element. a.remove(200) # Count elements with this value. a.count(900) # Print. print(a)array('i', [100, 900, 300])
I tested 2 programs. The first version uses an array of 10 million integers. And the second uses a list of those same integers. I measured the memory before the program exited.
from array import array # Create an empty int array. a = array("i") # Append ten million ints. for i in range(0, 10000000): a.append(i) # Finished. print("DONE") input()# Create an empty list. a = list() # Append ints. for i in range(0, 10000000): a.append(i) # Finished. print("DONE") input() 43.8 MB Array 710.9 MB List
Is a small array faster than an equivalent list? Should we optimize programs by using more arrays? Looping over small arrays is slower than looping over lists.
for
-loop over an array. We sum each value in the array.import time from array import array # Create an int array, and a list. list = list(range(0, 50)) arr = array("i", list) print(time.time()) # Version 1: loop over array. for i in range(0, 1000000): sum = 0 for value in arr: sum += value print(time.time()) # Version 2: loop over list. for i in range(0, 1000000): sum = 0 for value in list: sum += value print(time.time())1408894795.864125 [Python] 1408894803.765587 Array loop, 7.90 s 1408894811.303026 List loop, 7.53 s 1408894907.587 [PyPy3] 1408894908.066 Array loop, 0.48 s 1408894908.534 List loop, 0.47 s
For an integer array, we can use a flattened array from the array module. There is no syntax support for a 2D array directly, but we can use special indexes to create one.
Arrays are essential in programs that compute large numerical data sets. We witnessed the memory improvement of an array over a list.
We used array methods, which are similar (but not identical) to lists. For smaller data sets, lists are sufficient—and lists are more usable in most ways.