News for hackers. Stuff that matters.
Re:Why Do Python Lists Multiply Oddly? Exploring the CPython... by maxcoder4 on Tuesday April 09, 2024 @05:22AM (#39977657)
I think you're confused about the difference between [x]*4 and [x for _ in range (4)]. It's not, at any point, a difference between something being a value or reference.

The first code is equivalent to:

list_multiply([x], 4)

(the function is actually called list.__mul__, but that's an unimportant detail). The latter is a syntax sugar for:

out = [] for _ in range(4): out.append(x)

(actually a bit more complex, but close enough).

When you understand it, the behavior is obvious in both cases.

There are no comments on this comment yet.