tags:

views:

129

answers:

8

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

A: 
John Kugelman
That doesn't count as using a loop?
JacobM
Docs specify it as a way to initialize multidimensional list. For a one-dimension list the following initialisation is possible `list = [5] * 10`, but can't get it to work with more dimensions. Seems that when you do `list = [[5]*5]*5`, then each sub-dimension will point to the same memory, which is not what I want.
Leonid
+4  A: 

Sure, you can just do

mylist = [
            [1,2,3],
            [4,5,6],
            [7,8,9]
         ]
JacobM
You're absolutely right, and I'm not asking how that will look for a matrix 1000 X 1000...
Leonid
+4  A: 

Depending on your real needs, the de facto "standard" package Numpy might provide you with exactly what you need.

You can for instance create a multi-dimensional array with

numpy.empty((10, 4, 100))  # 3D array

or create the same arrays with zeros everywhere with

numpy.zeros((10, 4, 100))

Numpy is very fast, for array operations.

EOL
+1  A: 

I don't believe it's possible.

You can do something like this:

>>> a = [[0] * 5] * 5

to create a 5x5 matrix, but it is repeated objects (which you don't want). For example:

>>> a[1][2] = 1
[[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]]

You almost certainly need to use some kind of loop as in:

[[0 for y in range(5)] for x in range(5)]
+1  A: 

If you're doing numerical work using Numpy, something like

x = numpy.zeros ((m,n))
x = numpy.ones ((m,n))
Brian Hawkins
+2  A: 

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

gnibbler
Marking it as accepted as best reflects the question. numpy is not a part of standard Python installation.
Leonid
A: 

Recursion is your friend :D

It's a pretty naive implementation but it works!

dim = [2, 2, 2]

def get_array(level, dimension):
    if( level != len(dimension) ):
        return [get_array(level+1, dimension) for i in range(dimension[level])]
    else:
        return 0

print get_array(0, dim)
razpeitia
A: 

Python does not have arrays. It has other sequence types ranging from lists to dictionaries without forgetting sets - the right one depends on your specific needs.

Assuming your "array" is actually a list, and "initialize" means allocate a list of lists of NxM elements, you can (pseudocode):

  • for N times: for M times: add an element
  • for N times: add a row of M elements
  • write the whole thing out

You say you don't want to loop and that rules out the first two points, but why? You also say you don't want to write the thing down (in response to JacobM), so how would you exactly do that? I don't know of any other way of getting a data structure without either generating it in smaller pieces (looping) or explicitly writing it down - in any programming language.

Also keep in mind that a initialized but empty list is no better than no list, unless you put data into it. And you don't need to initialize it before putting data...

If this isn't a theoretical exercise, you're probably asking the wrong question. I suggest that you explain what do you need to do with that array.

Luke404
You're going to deep into some of the points :)) The point of the question is very simple and it asks what it asks for. I was wondering about different techniques people use for initialization of multidimensional **lists**, and the responses to the question perfectly reflect what I wanted to know. Some people use *de-facto* standard Numpy library. Some people initialize lists using `for` loops obviously. As *JacobM* suggests you can statically initialize the lists, but I point out that it's not applicable for large lists. *gnibbler* suggests very interesting but funky and non-efficient way.
Leonid
I was particularly wondering about initialization without `for` loops, as it was already obvious from the docs how you would do that one.
Leonid