0

I have noticed that some people use * to turn something into a list. I initially only knew of putting list(x) around the object, but doing *x also seems to work. Are they equivalent?

For example, both *Counter(list).values() and list(Counter(list).values()) work fine

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
user832075
  • 15
  • 6
  • 2
    Well, `*Counter(list).values()` doesn't work by itself (you'd need `[*Counter(list).values()]` to make a `list`), but yes, they're equivalent (aside from `[*iterable]` *always* making a `list`, while `list(iterable)` can do anything if someone did a dumb and used `list` as a variable name, shadowing the `list` constructor). – ShadowRanger Mar 02 '23 at 23:33
  • You can read all about this on [PEP 448: Additional Unpacking Generalizations](https://peps.python.org/pep-0448/). – ShadowRanger Mar 02 '23 at 23:35

0 Answers0