Python Forum
Strange behavior list of list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strange behavior list of list
#1
You can initialise a list containing 3 zeroes as follows:
mylist = 3*[0]

If you initialise a list containing 4 of such lists as follows:
mylist = 4*[3*[0]]

you get the following list of lists:
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

Now try to assign a value to the first element of the first list as follows:
mylist[0][0] = 1

When you print mylist you get:
[[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]]

Why is the value duplicatied 4 times?
I don't understand this.

If you define exactiy the same list by writing it out:
mylist2 = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

mylist2 looks the same as mylist initially. But now you can assign elements without the duplication.

It seems as if Pyhon creates a list of three items under water and creates the outer list as 4 references to this list.
Is there a way to avoid this without having to spell things out?
Reply
#2
This makes a list that contains three zeros. All the zeros are the same object.
mylist = 3*[0]
This makes a list of four lists. All the interior lists are the same object.
mylist = 4*[3*[0]]
It is exactly the same as if you did this.
zeros = 3 * [0]
mylist = 4 * [zeros] 
An easy way to avoid this is:
mylist = [3 * [0] for _ in range(4)]
"3 x [0]" is called 4 times, adding the result to mylist. This is a list comprehension, a kind of shorthand way of writing this:
mylist = []
for _ in range(4):
    mylist.append(3 * [0])
mmhmjanssen likes this post
Reply
#3
Here are two diagrams showing the internal structure of mylist and mylist2. As you can see, mylist contains 4 references to a single sublist while mylist2 contains 4 references to 4 independent sublist. (these pictures were produced by the educational module swampy.Lumpy showing live Python objects)
mmhmjanssen likes this post

Attached Files

Thumbnail(s)
       
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
Thanks to both of you. Makes sense.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  strange behavior of chess library in Python max22 1 381 Jan-18-2024, 06:35 PM
Last Post: deanhystad
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,305 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,650 May-01-2023, 09:06 PM
Last Post: deanhystad
  Program running on RPi 3b+ Very Strange Behavior - Out of Bound Index MadMacks 26 3,503 Mar-07-2023, 09:50 PM
Last Post: MadMacks
  List all possibilities of a nested-list by flattened lists sparkt 1 964 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,929 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,633 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,373 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Strange write()/File behavior kaega2 2 1,730 Jan-28-2022, 02:53 AM
Last Post: kaega2
  How to check if a list is in another list finndude 4 1,888 Jan-17-2022, 05:04 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020