Array ExamplesUse the array type to improve memory efficiency of numerical data.
This page was last reviewed on Nov 9, 2022.
Array. 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.
List, strings
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.
Constructor The array constructor receives 2 arguments here: the first is a code. The code "i" here signifies an integer array.
And In this example the second argument to array() is a list. The array will be made of integers equal to those specified in the list.
Finally We use a 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 We convert the array into a list with the tolist() method. We then join with an empty delimiter.
And This yields a 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, insert. Many 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.
Note We create an empty 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])
Memory. 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.
Result The array with 10 million integers required 43.8 MB of memory. The list version required 710.9 MB.
Per element For 10 million ints, the array used about 43 MB—about 4 bytes per integer. The list required closer to 70 bytes per element.
Warning These are just estimates. Many factors, including garbage collection overhead, are in play.
And One megabyte is not exactly 1 million bytes. It is 1,048,576 bytes. So the math is lacking.
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
Benchmark, array. 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.
Version 1 This version of the code uses a for-loop over an array. We sum each value in the array.
Version 2 Here we loop over a list. We print the time required for both versions of the code to complete.
Result Arrays are slower than lists for small collection sizes. Consider arrays only when large amounts of data is being stored.
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
2D arrays. 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.
2D Array
A summary. 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. The types are complementary. For smaller data sets, lists are sufficient. And lists are friendlier in most ways.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 9, 2022 (edit link).
© 2007-2023 Sam Allen.