Question

What's the best way to create 2D arrays in Python?

What I want is want is to store values like this:

X , Y , Z

so that I access data like X[2],Y[2],Z[2] or X[n],Y[n],Z[n] where n is variable. I don't know in the beginning how big n would be so I would like to append values at the end.

1
24
2/26/2014 12:11:04 AM

>>> a = []

>>> for i in xrange(3):
...     a.append([])
...     for j in xrange(3):
...             a[i].append(i+j)
...
>>> a
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
>>>
22
5/13/2009 9:29:01 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon