Search code examples
pythonlisttypesunpackiterable-unpacking

Why does unpacking of `*a, b = something` makes `a` always `list` type?


I was just testing around with unpacking,

And I realized:

*a, b = {1, 2, 3}

Makes a a list, not a set.

With it as:

[1, 2]

And I tried:

*a, = {1, 2, 3}

Expecting a set, but it still becomes a list as:

[1, 2, 3]

And even with tuples:

*a, = (1, 2, 3)

It does the same list thing as set does.

This doesn't seem right to me for some reason, I think this is on purpose.


Solution

  • Possible changes discussed were:

    [...]

    Try to give the starred target the same type as the source iterable, for example, b in a, *b = 'hello' would be assigned the string 'ello'. This may seem nice, but is impossible to get right consistently with all iterables.

    Source: https://www.python.org/dev/peps/pep-3132/