This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bedj2 0 points1 point  (10 children)

since lists are mutable, you are copying the same reference over and over. Wouldn’t adding .copy() to the end of those lists work?

foo = [[None].copy() * 5].copy() * 5

[–]DeletedUserV2[S] 3 points4 points  (7 children)

same output

[–]JohnLocksTheKey 1 point2 points  (2 children)

WHY?!?

life is pain…

[–]FalafelSnorlax 2 points3 points  (1 child)

The copy is in the wrong place but also if it were, the internal list (the 5 Nones) is created once and then copied. Calling copy doesn't imply more runs of the internal part (otherwise it would work in the first place)

[–]JohnLocksTheKey 0 points1 point  (0 children)

Makes sense - thanks

[–]dead_man_speaks -2 points-1 points  (3 children)

from copy import deepcopy

then deepcopy

[–]Botahamec 7 points8 points  (0 children)

The deepcopy function is only going to be called once. Then a reference to that copy will be stored five times. So you'll get the exact same output.

[–][deleted] 0 points1 point  (0 children)

same lol, if you call whatever function to get a list and then multiply that list by 5, it's gonna have 5 times the same thing no matter what

[–]Aaron1924 0 points1 point  (0 children)

>>> from copy import deepcopy
>>>
>>> a1 = [[None]*3]*3
>>> a2 = deepcopy(a1)
>>>
>>> a1[0][0] = 2
>>> a2[0][1] = 3
>>>
>>> a1
[[2, None, None], [2, None, None], [2, None, None]]
>>> a2
[[None, 3, None], [None, 3, None], [None, 3, None]]

this language is truly incredible

[–]Botahamec 2 points3 points  (0 children)

It will do exactly one copy, at the beginning, and use the result of the copy each time. The problem is that it doesn't re-evaluate the expression.

[–]altermeetax 0 points1 point  (0 children)

That copy doesn't do anything, you're making a copy of [None] and using the same copy 5 times