Home > OS >  Creating a list of lists via iteration - Cannot understand why variable.append() gives me the same l
Creating a list of lists via iteration - Cannot understand why variable.append() gives me the same l

Time:01-08

I would like to create a list of lists, with each sub-list increasing by 1.

In other words

[[1],
 [1, 2],
 [1, 2, 3],
 [1, 2, 3, 4],
 [1, 2, 3, 4, 5],
 [1, 2, 3, 4, 5, 6],
 [1, 2, 3, 4, 5, 6, 7],
 [1, 2, 3, 4, 5, 6, 7, 8],
 [1, 2, 3, 4, 5, 6, 7, 8, 9],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]

I wrote the following code, but I don't understand why list_of_lists returns the same thing 10 times in a row.

I have found examples of how to do it correctly, but I would like to understand why the code below appends the same version of the temporary list to list_of_lists, rather than appending each new version once.

list_of_lists = []
temporary = []
for i in range(1,11):
    temporary.append(i)
    print(temporary)
    # the temporary list iterates as expected when I print it. When i is 1, it prints [1]. When i is 2, it  prints [1,2]
    list_of_lists.append(temporary)
    print(list_of_lists)
    # "list of lists" simply appends the LATEST version of "temporary" list i number of times.

My reasoning is that list_of_lists.append(temporary) should simply append 1 version of the temporary list to the end. And I don't understand why instead it overwrites the previous appended versions of the temporary list, and doesn't simply append the latest one to the already existing entries in list_of_lists

I have seen examples of how to do it correctly, so this issue is functionally solved. But I would greatly appreciate it if anyone could please take the time to explain why my logic was incorrect.

Thank you.

I was expecting:

[[1],
 [1, 2],
 [1, 2, 3],
 [1, 2, 3, 4],
 [1, 2, 3, 4, 5],
 [1, 2, 3, 4, 5, 6],
 [1, 2, 3, 4, 5, 6, 7],
 [1, 2, 3, 4, 5, 6, 7, 8],
 [1, 2, 3, 4, 5, 6, 7, 8, 9],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]

I got:

[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]

CodePudding user response:

instead of:

list_of_lists.append(temporary)

you can use this:

list_of_lists.append(temporary[:])

The difference ist, that the first line only appends a reference to the list, where the second line creates a copy through [:] of the list.

CodePudding user response:

If you use list_of_lists.append(temporary), you only add a reference to the temporary list. What this means is when you print list_of_lists, it checks to see the latest version of the temporary list, and prints that. Therefore, any changes made in temporary will automatically be made in list_of_lists.

To get aroudn this, instead of appending the temporary list, you can append a copy of the list instead with temporary.copy().

Try this:

list_of_lists = []
temporary = []
for i in range(1,11):
    temporary.append(i)
    print(temporary)
    list_of_lists.append(temporary.copy()) #this should fix it
    print(list_of_lists)

This then outputs:

[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
  • Related