MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/Python/comments/gzila7/python_3_in_one_pic/fthp5rk
r/Python • u/TheInsaneApp • Jun 09 '20
167 comments sorted by
View all comments
Show parent comments
16
Wasn't in py2 but it's useful, can be more complex too like:
a, b, *others, c = [1, 2, 3, 4, 5, 6]
With others getting [3, 4, 5]
1 u/OPtoss Jun 09 '20 Can you do*others, last = [1,2,3] ?Orfirst, *others, last = [1,2,3,4] ? 10 u/Ph0X Jun 09 '20 Yep! You can have one catch-all and as many individual unpacking on either side. That being said if the size of the iterable is smaller than the number of targets, it'll fail. But the catch-all can be empty so this worksa, *rest = [1]
1
Can you do
*others, last = [1,2,3] ?
*others, last = [1,2,3]
Or
first, *others, last = [1,2,3,4] ?
first, *others, last = [1,2,3,4]
10 u/Ph0X Jun 09 '20 Yep! You can have one catch-all and as many individual unpacking on either side. That being said if the size of the iterable is smaller than the number of targets, it'll fail. But the catch-all can be empty so this worksa, *rest = [1]
10
Yep! You can have one catch-all and as many individual unpacking on either side. That being said if the size of the iterable is smaller than the number of targets, it'll fail. But the catch-all can be empty so this works
a, *rest = [1]
16
u/Ph0X Jun 09 '20
Wasn't in py2 but it's useful, can be more complex too like:
a, b, *others, c = [1, 2, 3, 4, 5, 6]With others getting [3, 4, 5]