You can do it quite efficiently with a list comprehension:
a = [[0] * number_cols for i in range(number_rows)]
Answer from cheeyos on Stack OverflowYou can do it quite efficiently with a list comprehension:
a = [[0] * number_cols for i in range(number_rows)]
This is a job for...the nested list comprehension!
[[0 for i in range(10)] for j in range(10)]
Multidimensional arrays?
Performance advise for managing a large multi-dimensional list in Python.
New to python but not other higher level languages.
I’m having difficulty understanding how to use multidimensional lists in python.
I have a list if 256 objects. Each object has 6 variables. In other languages, I could read and update/change similar to
object[0] = [1,2,3,4,5,6]
…
object[255] = [1,2,3,4,5,6] Etc.
I understand in python this isn’t possible, so I’ll have to revert to something similar to
object = [[0], [1,2,3,4,5,6]]
…
object = [[255], [1,2,3,4,5,6]]
And reading will be similar to
var1 = object[0][3]
print(var1)
But I can’t get my head around it or find easy to,understand examples.
Does anyone have any quick and easy to digest resources that could help me understand this a bit better?
Thanks!
Hello!
I have 5 sets, each set contains a number of elements of variable length (eg. set 1 has 100 elements, set 2 has 150 and so on)
Is it possible to store these sets in a single structure?
For example, if I want to print the 35th elements of the 3rd set, I could call it simply by saying something like MyContainer[setN][elementN]
I was trying to use a 2D array but I can't initialize its shape, since each set has a variable number of elements.
Can anybody point me to the right direction / best practice?
Thank you