Python Forum
Appending to list of list in For loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Appending to list of list in For loop
#1
Hello,

I have question regarding for loops and appending to a list in that loop. In a for loop, I am trying to append a list to a list of lists. With each iteration, I modify one of the elements of the list. The code is listed below:

test = []
current = [0, 0]

for i in range(6):
    print('current value: {}'.format(current))
    test.append(current)
    current[0] += 1 #only increment the value at index 0

print('Test: {}'.format(test))
I print the current value with every iteration for testing purposes. The desired output is Test: [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0]]. However, running the code yields the following:
Output:
current value: [0, 0] current value: [1, 0] current value: [2, 0] current value: [3, 0] current value: [4, 0] current value: [5, 0] Test: [[6, 0], [6, 0], [6, 0], [6, 0], [6, 0], [6, 0]]
It seems like the current value is correctly incremented, however the final output (test) is a list of lists containing only the last value taken by current.

Can anyone explain this behaviour, or how I could get the desired output?

Thanks,
Nicolas
Reply
#2
current is a (mutable) list. When you append it to test each time, you are appending the same object. When you change one of the values of that object on line 7, all the views of that object are changed also.

You want to have 6 different lists in test, not 6 instances of the same list.

You could append a copy of that list. Some options:
from copy import copy
test.append(copy(current))
# or
test.append(list(current))
Reply
#3
Thanks for your reply! Makes sense now and fixed my code!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 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,714 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  convert this List Comprehensions to loop jacklee26 8 1,418 Oct-21-2022, 04:25 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,499 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 3,837 May-11-2022, 02:23 PM
Last Post: menator01
  Split a number to list and list sum must be number sunny9495 5 2,197 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Loop through a list of string oldtrafford 4 1,421 Mar-24-2022, 05:30 PM
Last Post: deanhystad
  Help add for loop results in a list paulo79 4 1,488 Mar-09-2022, 05:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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