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
  knowing for loop position in a list medic5678 4 655 Jan-31-2025, 04:19 PM
Last Post: perfringo
  For Loop assigns only the latest value from List Caliban86 3 1,131 Sep-22-2024, 02:47 AM
Last Post: deanhystad
  Strange behavior list of list mmhmjanssen 3 1,534 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 2,555 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 3,274 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 1,739 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 3,311 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  convert this List Comprehensions to loop jacklee26 8 2,774 Oct-21-2022, 04:25 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 2,570 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 6,404 May-11-2022, 02:23 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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