Skip to content
Advertisement

Python multi-dimensional array initialization without a loop

Is there a way in Python to initialize a multi-dimensional array / list without using a loop?

Advertisement

Answer

Sure there is a way

arr = eval(`[[0]*5]*10`)

or

arr = eval(("[[0]*5]+"*10)[:-1])

but it’s horrible and wasteful, so everyone uses loops (usually list comprehensions) or numpy

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement