views:

178

answers:

5

I'm trying to create a 3-dimensional N*N*N list in Python, like such:

n=3
l = [[[0,]*n]*n]*n

Unfortunately, this does not seem to properly "clone" the list, as I thought it would:

>>> l
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
>>> l[0][0][0]=1
>>> l
[[[1, 0, 0], [1, 0, 0], [1, 0, 0]], [[1, 0, 0], [1, 0, 0], [1, 0, 0]], [[1, 0, 0], [1, 0, 0], [1, 0, 0]]]

What am I doing wrong here?

+2  A: 

It's not cloning the list. It's inserting a reference to the same list over and over. Try creating the list using a set of nested for loops.

Dan Lorenc
+5  A: 

The problem is that * n does a shallow copy of the list. A solution is to use nested loops, or try the numpy library.

Tordek
+2  A: 

As others have mentioned, it's building the 2nd and 3rd levels with references, not clones. Try:

>>> n = 3

>>> l = [[[0]*n for _ in xrange(n)] for _ in xrange(n)]

>>> l[0][0][0] = 1

>>> l
[[[1, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]

Or if you want to type a bit less:

>>> l = [[[0]*n for _ in '.'*n] for _ in '.'*n]
Steve Losh
+3  A: 

If you want to do numerical processing with 3-d matrix you are better of using numpy. It is quite easy:

>>> import numpy
>>> numpy.zeros((3,3,3), dtype=numpy.int)
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]])
>>> _[0,0,0]
0
Leonardo Santagada
+1  A: 

I have to second what leonardo-santagada suggested, with the addition that creating N dimensional arrays/lists is very unpythonic and you should reconsider how you're keeping your data and seeing if it doesn't belong better in a class or a list of dictionaries (or dictionaries of lists).

Daniel Goldberg
Thank you for pointing this out. I needed this to create a 3-dimensional tic-tac-toe board for an AI algorithm. While it's true that numpy arrays might have sped up some operations, I figured this would be "good enough" for this small program.
Matt Boehm