C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
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
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.
joinPython 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
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])
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.
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
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.
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
Tip: A good developer should reference all helpful websites, not just ones with pictures.
Quote: Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained.
Efficient arrays of numeric values: Python.org