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]]