-2

Let's say that I make a 3D list

list = [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]

and I run

list[0][0][0] = 1 #set the first element of the first list of the first list to 1

print(list)

I'd expect to get

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

but instead, I get

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

Can someone figure out how to make it assign a variable to ONLY ONE element of a 3D list, instead of every first element? Thanks!

If it matters, I'm using Python 3.7 32-bit.

  • 2
    I get what you expected, not what you got. Is that your exact code? – joel May 14 '19 at 17:53
  • 1
    How are you building your `list`? Are you declaring it literally like that? Or are you doing something like `list = [[a,a], [a,a]]`? In that case, you are assigning the *same* list `a` to each element, so when you change it, you see that change everywhere `a` is referenced. – gen_Eric May 14 '19 at 17:54
  • P.S. Don't name your list `list`. Use a different variable name. Doing this will mask the built-in `list` causing interesting issues later in your program. Like being unable to do `list(something)`... – gen_Eric May 14 '19 at 17:57
  • If you plan to use a lot of 3D lists holding same type values (for example `int`s) I suggest you to consider usage of `numpy` (take look at `numpy.array`) – Daweo May 14 '19 at 18:00
  • Because you did not create the list the way you are showing, rather, you made a list which references the same list internally. Note, *lists don't have dimensions* – juanpa.arrivillaga May 14 '19 at 18:06
  • 1
    If you've built your list with some sort of replicator, then you've stumbled into a well-documented problem. https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly – Prune May 14 '19 at 18:10
  • 1
    Turns out that Rocket Hazmat and juanpa.arrivillaga were right! I was using a different piece of code to make the list, which was ([([[0,0,0]])*size_x]*size_y) . Hoog's code fixed it. – A Canadian May 14 '19 at 18:20
  • 2
    In future, please test the code you post before posting. – Blorgbeard May 14 '19 at 18:26

1 Answers1

0

I have reproduced your results by making an assumption about how you actually defined your list. I assume that you defined some variable such as ab below and used that to create your list. However, the new list is still a bunch of references to your ab variable, so changing one actually changes ab which will affect your whole list.

ab = [0,0]

mylist = [[ab,ab],[ab,ab]]
mylist[0][0][0] = 1

print(mylist,"    ",ab)

OUTPUT

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

To resolve this, simple initialize your lists with 0 instead of some variable:

mylist = [[[0,0],[0,0]],[[0,0],[0,0]]]

or

mylist = [[[0 for _ in range(2)] for _ in range(2)] for _ in range(2)]
Hoog
  • 2,280
  • 1
  • 14
  • 20
  • Thank you! This worked for me. It turns out that, as Rocket Hazmat pointed out, I had somehow managed to declare the same list multiple times in the same list... Sounds like some wormhole type math to me. Declaring it using the last method you gave worked. – A Canadian May 14 '19 at 18:18