1

I did this code down below and i was expecting the output of the code to return as a tuple.

li = ('a', 'b', 'c', 'd')

(e, f, *g) = li

print(g)

But the output was

['c', 'd']

What does "(e, f, *g) = li" actually mean and why did the code return as a list?

  • 3
    Because that is what [the specification in PEP 3132](https://www.python.org/dev/peps/pep-3132/#specification) says: "A tuple (or list) on the left side of a simple assignment [...] may contain at most one expression prepended with a single asterisk (which is henceforth called a "starred" expression, while the other expressions in the list are called "mandatory"). This designates a subexpression that will be assigned a *list* of all items from the iterable being unpacked that are not assigned to any of the mandatory expressions, or an empty list if there are no such items." – 9769953 Apr 20 '21 at 09:21
  • @python_user While close enough, the linked answer doesn't mention why unpacking a tuple turns into a list. – 9769953 Apr 20 '21 at 09:25
  • @python_user Ah, I had overlooked that paragraph. – 9769953 Apr 20 '21 at 09:29

0 Answers0