How do I define an array in python?
5 answers
There are several types of arrays in Python if you want a classic array with an array unit:
import array
a = array.array('i', [1,2,3])
But you can also use tuples without having to import other modules:
t = (4,5,6)
Or lists:
l = [7,8,9]
A Tuple is more efficient to use but has a fixed size, while you can easily add new items to lists:
>>> l.append(10)
>>> l
[7, 8, 9, 10]
>>> t[1]
5
>>> l[1]
8
+3
source to share