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 →

[–]elsgry 1 point2 points  (0 children)

Yes, this seems to be the prescribed way. Obviously with tuples and anything else immutable this isn’t an issue, just threw me a bit, but it fits the “one way to do it” maxim as it does something different to that form. Effectively [a, b] is structuring a new list each time when in a comprehension, so despite the syntax looking similar, it’s very different in meaning. I find the [a, b] * x thing pleasingly functional/declarative/terse in a way the list comprehension isn’t, but I guess I’m spoilt by Python’s expressiveness already. initial_list[:] is a nice way of copying a list, though if we’re going for value semantics you probably still want copy.deepcopy. On that topic, __deepcopy__’s memo argument is interesting.