Python Forum
Lists inside lists work strange
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lists inside lists work strange
#1
I've got a problem where I use .append() to add coordinates, in the form of [x,y] to a list, then change the coords, add it again, and so on. But when I try to use the list for something, all the indexes where I added that coords have the current value of the coords, not the one I added.
I could paste the code but it's really large and uses Pygame, so I'll make an example of what happens.

a = [[0,0],2,[1,2]]
b = [0,0]
while b[0]<100:
        a.append(b)
        b[0] += 1
        b[1] += 1
print(a)
This should print increasing numbers in sets of two, but instead it prints [[0,0],2,[1,2],[100,100],[100,100],[100,100]...], so it appears that the values inside the list change with the coords. I thought that this was probably a feature of lists, that is, refreshing its values constantly, but then I wrote this

a = [5,2,6]
b = 0
while b<100:
        a.append(b)
        b+=1
print(a)
and this does print [5,2,6,0,1,2,3,4,5,6...]. So it appears to have something to do with .append()ing lists to lists.

Can anyone tell me is this is intended or if it's an issue, and how to get around it to have the first code work the same way as the second?
Thanks in advance.
Reply
#2
You are appending the object-b to object-a, but what you really want to append the contents of the object-b. This can be accomplished by assigning the contents of object-b to a new-object each time and then append the new-object to object-a, or append 'b[:]', a copy of the contents of object-b to object-a.
Reply
#3
(Jun-07-2018, 01:45 AM)coder19 Wrote: You are appending the object-b to object-a, but what you really want to append the contents of the object-b.

Allow me to elaborate on that point. A variable name in Python is a reference to an object, so what OP is doing is adding reference to the same object over and over again, and then changing that object. I have reduced the counter to 5, and printed addresses of a elements to demonstrate

Output:
In [10]: print(', '.join(str(id(o)) for o in a)) 139693607995528, 10943040, 139693607556040, 139693607963272, 139693607963272, 139693607963272, 139693607963272, 139693607963272 In [11]: id(b) Out[11]: 139693607963272
As you may see, a contains 5 references to object b - and you get what you get.

While copying list content may be a sound advice in general, in this case I would suggest to drop b altogether - it is inefficient approach

a.extend([i, i] for i in range(100))


PS Please, get into a habit of using meaningful names for your variables. One- or two-letter variables (unless in list comprehension, and even then questionable) don't tell the reader of your code their purpose and get lost in larger blocks of code
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
Ok, so what I've understood from your replies, appending adds a reference to a variable, not the value itself. I'll have to try to work around it. But my question is, in my first post, why doesnt this happen with the second code?
Reply
#5
(Jun-07-2018, 07:16 AM)jdrp Wrote: But my question is, in my first post, why doesnt this happen with the second code?

Integers are immutable - increment creates a new object. By changing element in a list you change list content - not list object

Output:
In [23]: b = 2000 In [24]: id(b) Out[24]: 139693577346320 In [25]: b += 1 In [26]: id(b) Out[26]: 139693577346192
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
Ok I managed to work around it just by setting int variables to each of the list elements and then using those. Thanks for your replies

I'm new to this forum. How do i mark this as solved?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 1 10 29 minutes ago
Last Post: menator01
  problem with print lists MarekGwozdz 4 676 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,535 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 758 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 757 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 801 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,423 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 739 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,270 Apr-13-2023, 06:40 AM
Last Post: Dato
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 765 Feb-28-2023, 10:55 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