A list in Python can store these values. But it uses excessive memory.
For efficiency, the array type in Python is better. We use arrays. We test them and measure performance. Code with arrays is more complex.
An example. Here we start. Please add the "from import" statement at the top of the program. This lets you access the array type.
Constructor: The array constructor receives two 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.
Based on: Python 3 Python program that creates int array 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) Output 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.
Python program that creates char array 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) Output 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.
Python program that uses append, insert, remove, count 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) Output array('i', [100, 900, 300])
Memory. I tested two programs. The first version uses an array of ten 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.
Memory of elements. We can estimate how much each element used. For 10 million ints, the array used about 43 MB—about 4 bytes per integer. The list required closer to 70 bytes per element.
Caution: 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.
Python program that uses int array 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() Python program that uses int list # Create an empty list. a = list() # Append ints. for i in range(0, 10000000): a.append(i) # Finished. print("DONE") input() Memory usage 43.8 MB Array 710.9 MB List
Array benefits. Performance and memory usage were both vastly improved with the array. In testing this program, the speed of array is also much better.
Thus: Using an array instead of a list in a numerical program could make the program many times faster.
And: It could make a program that was previously unusable, usable. It is vastly superior in some contexts.
However: Avoid premature optimization. The performance benefits of arrays will not be measurable on smaller collections.
Speed. Is a small array faster than an equivalent list? Should we optimize programs by using more arrays? I found that looping over small arrays is actually slower than looping over lists.
Thus: Arrays are slower than lists for small collection sizes. We reduce speed by using arrays on small amounts of data.
But: For large amounts of data, an array will use less memory and this in turn improves performance. It improves memory locality.
Python program that uses int array, list 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()) Output 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
Codes, bytes. There are many type codes available on arrays. Please consult the official Python documentation for a descriptive table. The number of bytes for each element type is shown.
Tip: A good developer should reference all helpful websites, not just ones with pictures.
Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained.
Essential. 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.