Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
Why does starred assignment produce lists and not tuples?

In python, I can write something like this:

some_list = [(1, 2, 3), (3, 2, 1)]

for i, *args in some_list:
   print(args)

I will get the next output:

[2, 3]
[2, 1]

When we use *args as function arguments, it is unpacked into a tuple.

Why do we receive a list in this situation?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It is just a design decision. Making it a tuple was debated in the PEP 3132, but rejected on usability grounds:

Make the starred target a tuple instead of a list. This would be consistent with a function's *args, but make further processing of the result harder.

Simlarly, making it of the same type as the iterable on the rhs of the assignment, was rejected:

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.

The very example of yours is listed in the same PEP under specification.

Some reasoning is found in the mailing list of that debate.

When dealing with an iterator, you don't know the length in advance, so the only way to get a tuple would be to produce a list first and then create a tuple from it.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!