Why Nostr? What is Njump?
2024-08-05 16:35:08

Trey Hunner 🐍 on Nostr: Need to copy an iterable in #Python? I usually recommend using the list constructor. ...

Need to copy an iterable in #Python? I usually recommend using the list constructor.

old = [2, 1]
new = list(old)

If you're CERTAIN you have a list, you could use copy:

new = old.copy()

I don't recommend slicing because it's hard to read 😐:

new = old[:]

The [*...] syntax works, but I find it less explicit than list(...):

new = [*old]

If you're accepting ANY iterable (duck typing! 🦆), I'd copy using list(...).

More on when I prefer list(...) and when I don't: pym.dev/using-list
Author Public Key
npub106g4n7la25nyzhyd9h3wf3m9fcmxpzplnn9wwg98p73uru70razsjsuas2