r/Python • u/Adrewmc • Apr 15 '26
Discussion What if we hade slicing unpacking for tuples
The issue
my_tup = (1,2,3)
type_var, *my_list = my_tup
This means tuple unpacking create two new types of objects.
My solution is simple. Just add tuple to the assignment.
(singlet_tup, *new_tup) = my_tup
Edit:
I think this is clearer, cleaner and superior syntax than I started with. my_tup should be consider as an object that can be unpacked. And less capable of breaking old code.
type_var, *as_list = my_tup
type_var, *(as_tup) = my_tup
type_var, *{as_set} = my_tup
type_var, *[as_list] = my_tup
The (*) unpacks to a list unless otherwise asked to upon assignment, Is my (new) proposal. Which seems much more reasonable.
This is similar to the difference of (x for x in iterator) and [x for x in iterator] and {x for x in iterator} being comprehended syntax. A ‘lazy” object would be fine.
End edit.
Notice : my_list vs. new_tup change here
This should give the equivalent to a
singlet_tup, *new_tup = my_tuple[0], my tuple[1:]
Using a tuple syntax in assignment forces the unpacking to form as a tuple instead.
Is this a viable thing to add to Python. There are many reason you might want to force a tuple over a list that are hard to explain.
Edit: I feel I was answered. By the comment below.
https://www.reddit.com/r/Python/s/xSaWXCLgoR
This comment showed a discussion of the issue. It was discussed and was decided to be a list. The fact that there was debate makes me feel satisfied.
5
u/Gnaxe Apr 15 '26
Lists do make more sense here, because you can unpack an iterator which may not have a length known in advance. If you still want a tuple after that, you can convert a list. If you do know the length, you can use explicit slices or
itertools.islice()s.3
u/coked_up_werewolf Apr 15 '26
Here is the pep that introduces the syntax. https://peps.python.org/pep-3132/#acceptance
There is some discussion around it returning a list or tuple in the referenced discussion (need to click through the email chain) https://mail.python.org/pipermail/python-3000/2007-May/007198.html
4
u/johndburger Apr 15 '26 edited Apr 15 '26
I’m not exactly sure what you’re suggesting here, but FYI parentheses are not the tuple syntax, commas are.
This already has a tuple on the left-hand side.Edit: This isn’t true as pointed out below.
1
u/MathMXC Apr 15 '26
So this isn't really related to your suggestion but you mention that it's "hard to explain" why you'd use a tuple over a list. IMO it's not hard (generally immutably and speed). What makes you think it's hard?
Regarding your suggestion: what is the real use case here? Is it just syntax sugar? If so it seems more confusing then anything
4
u/__Raptor__ Apr 15 '26
You need to go take a Python for Beginners tutorial or something because what you have said here makes no sense at all.
1
u/Kerbart Apr 15 '26
So if you have
What are a and b going to be?
(1, 2)and(3, 4, 5)? or(1, 2, 3)and(4, 5)1
u/undercoveryankee Apr 15 '26
This was discussed on the mailing list when PEP 3132 was pending.
The designers decided that unpacking should work for any iterable on the right-hand side without depending on anything outside the iterable protocol, and you should be able to reason about the results of unpacking even if you don't know what types of iterables your callers might give you.
They chose to have the starred target receive a list because (1) they felt that it would be the more common case, and (2) there's no efficient way to construct a tuple from an iterable with no
__len__without using an intermediate list.Since it won't always be possible to construct a tuple without constructing a list first, explicit is better than implicit. I'd rather do
than add more significant punctuation to the grammar for unpacking.