News for hackers. Stuff that matters.
Re:Why Do Python Lists Multiply Oddly? Exploring the CPython... by zarzavat on Tuesday April 09, 2024 @07:44AM (#39978421)
Lists in Python are always references.

There’s a separate type called tuple specifically designed to represent lists-as-values.

If you do [()]*4 then you get exactly what you want.

Re:Why Do Python Lists Multiply Oddly? Exploring the CPython... by dragonwriter on Tuesday April 09, 2024 @12:45PM (#39981270)
> There’s a separate type called tuple specifically designed to represent lists-as-values.

tuples are also references, but to an immutable object.

> If you do [()]*4 then you get exactly what you want.

I think what is actually wanted here is four different mutable lists rather than the same mutable list four times, something like what you would get from:

  [[] for _ in range(4)]