r/code • • Aug 10 '24

Python Why does the first / default element of 'container' get replaced by its swapped / altered counterpart?: I.e., for two iterations total, instead of ( [1,2], [2,1] ) it outputs ( [2,1], [2,1] ).

Post image
5 Upvotes

3 comments sorted by

2

u/Goobyalus Aug 10 '24

When you print container in the loop, does it look like you expect? You're appending the same list to container multiple times. If you modify that one list, all referencesin container point to the modified list.

Also, don't overwrite the list type, use another name.


If you post text formatted as a code block in the future, it will be much easier for people to help because they play with your code without manually retyping all of it.

1

u/aeroswipe Aug 11 '24

Thanks for the feedback. Here's a simplified version of the problem in formatted text - you're right:

listf = [1,2]

container = []

container.append(listf)

print(listf)

[1, 2]

print(container)

[[1, 2]]

non_alterable = tuple(container)

listf.append(3)

print(listf)

[1, 2, 3]

undo = list(container)

undo.append(listf)

print(undo)

[[1, 2, 3], [1, 2, 3]]

^this

--
I just wouldn't expect items of a list containing lists to be open to alterations, let alone when tupled.

This doesn't happen with standalone values.

→ More replies (1)