Extended iterable unpacking ("Starred assignment", Python 3 only)
The best way to explain this is via an example:
>>> a,*b,c=range(5)
>>> a
0
>>> b
[1, 2, 3]
>>> c
4
We've already seen a use for this — turning an iterable into a list in Python 3 turning an iterable into a list in Python 3:
a=list(range(10))
*a,=range(10)
Here are a few more uses.
Getting the last element from a list
a=L[-1]
*_,a=L
In some situations, this can also be used for getting the first element to save on parens:
a=(L+[1])[0]
a,*_=L+[1]
Assigning an empty list and other variables
a=1;b=2;c=[]
a,b,*c=1,2
Removing the first or last element of a non-empty list
_,*L=L
*L,_=L
These are shorter than the alternatives L=L[1:] and L.pop(). The result can also be saved to a different list.
Tips courtesy of @grc
Extended iterable unpacking ("Starred assignment", Python 3 only)
The best way to explain this is via an example:
>>> a,*b,c=range(5)
>>> a
0
>>> b
[1, 2, 3]
>>> c
4
We've already seen a use for this — turning an iterable into a list in Python 3:
a=list(range(10))
*a,=range(10)
Here are a few more uses.
Getting the last element from a list
a=L[-1]
*_,a=L
In some situations, this can also be used for getting the first element to save on parens:
a=(L+[1])[0]
a,*_=L+[1]
Assigning an empty list and other variables
a=1;b=2;c=[]
a,b,*c=1,2
Removing the first or last element of a non-empty list
_,*L=L
*L,_=L
These are shorter than the alternatives L=L[1:] and L.pop(). The result can also be saved to a different list.
Tips courtesy of @grc
Extended iterable unpacking ("Starred assignment", Python 3 only)
The best way to explain this is via an example:
>>> a,*b,c=range(5)
>>> a
0
>>> b
[1, 2, 3]
>>> c
4
We've already seen a use for this — turning an iterable into a list in Python 3:
a=list(range(10))
*a,=range(10)
Here are a few more uses.
Getting the last element from a list
a=L[-1]
*_,a=L
In some situations, this can also be used for getting the first element to save on parens:
a=(L+[1])[0]
a,*_=L+[1]
Assigning an empty list and other variables
a=1;b=2;c=[]
a,b,*c=1,2
Removing the first or last element of a non-empty list
_,*L=L
*L,_=L
These are shorter than the alternatives L=L[1:] and L.pop(). The result can also be saved to a different list.
Tips courtesy of @grc
Extended iterable unpacking ("Starred assignment", Python 3 only)
The best way to explain this is via an example:
>>> a,*b,c=range(5)
>>> a
0
>>> b
[1, 2, 3]
>>> c
4
We've already seen a use for this — turning an iterable into a list in Python 3:
a=list(range(10))
*a,=range(10)
Here are a few more uses.
Getting the last element from a list
a=L[-1]
*_,a=L
In some situations, this can also be used for getting the first element to save on parens:
a=(L+[1])[0]
a,*_=L+[1]
Assigning an empty list and other variables
a=1;b=2;c=[]
a,b,*c=1,2
Removing the first ofor last element of a non-empty list
_,*L=L
*L,_=L
These are shorter than the alternatives L=L[1:] and L.pop(). The result can also be saved to a different list.
Tips courtesy of @grc
Extended iterable unpacking ("Starred assignment", Python 3 only)
The best way to explain this is via an example:
>>> a,*b,c=range(5)
>>> a
0
>>> b
[1, 2, 3]
>>> c
4
We've already seen a use for this — turning an iterable into a list in Python 3:
a=list(range(10))
*a,=range(10)
Here are a few more uses.
Getting the last element from a list
a=L[-1]
*_,a=L
In some situations, this can also be used for getting the first element to save on parens:
a=(L+[1])[0]
a,*_=L+[1]
Assigning an empty list and other variables
a=1;b=2;c=[]
a,b,*c=1,2
Removing the first of last element of a non-empty list
_,*L=L
*L,_=L
These are shorter than the alternatives L=L[1:] and L.pop(). The result can also be saved to a different list.
Tips courtesy of @grc
Extended iterable unpacking ("Starred assignment", Python 3 only)
The best way to explain this is via an example:
>>> a,*b,c=range(5)
>>> a
0
>>> b
[1, 2, 3]
>>> c
4
We've already seen a use for this — turning an iterable into a list in Python 3:
a=list(range(10))
*a,=range(10)
Here are a few more uses.
Getting the last element from a list
a=L[-1]
*_,a=L
In some situations, this can also be used for getting the first element to save on parens:
a=(L+[1])[0]
a,*_=L+[1]
Assigning an empty list and other variables
a=1;b=2;c=[]
a,b,*c=1,2
Removing the first or last element of a non-empty list
_,*L=L
*L,_=L
These are shorter than the alternatives L=L[1:] and L.pop(). The result can also be saved to a different list.
Tips courtesy of @grc
Extended iterable unpacking ("Starred assignment", Python 3 only)
The best way to explain this is via an example:
>>> a,*b,c=range(5)
>>> a
0
>>> b
[1, 2, 3]
>>> c
4
>>> a,*b,c=range(5)
>>> a
0
>>> b
[1, 2, 3]
>>> c
4
We've already seen a use for this — turning an iterable into a list in Python 3:
a=list(range(10))
*a,=range(10)
a=list(range(10))
*a,=range(10)
Here are a few more uses.
Getting the last element from a list
a=L[-1]
*_,a=L
a=L[-1]
*_,a=L
In some situations, this can also be used for getting the first element to save on parens:
a=(L+[1])[0]
a,*_=L+[1]
a=(L+[1])[0]
a,*_=L+[1]
Assigning an empty list and other variables
a=1;b=2;c=[]
a,b,*c=1,2
a=1;b=2;c=[]
a,b,*c=1,2
Removing the first of last element of a non-empty list
_,*L=L
*L,_=L
These are shorter than the alternatives L=L[1:] and L.pop(). The result can also be saved to a different list.
Tips courtesy of @grc
Extended iterable unpacking ("Starred assignment", Python 3 only)
The best way to explain this is via an example:
>>> a,*b,c=range(5)
>>> a
0
>>> b
[1, 2, 3]
>>> c
4
We've already seen a use for this — turning an iterable into a list in Python 3:
a=list(range(10))
*a,=range(10)
Here are a few more uses.
Getting the last element from a list
a=L[-1]
*_,a=L
In some situations, this can also be used for getting the first element to save on parens:
a=(L+[1])[0]
a,*_=L+[1]
Assigning an empty list and other variables
a=1;b=2;c=[]
a,b,*c=1,2
Tips courtesy of @grc
Extended iterable unpacking ("Starred assignment", Python 3 only)
The best way to explain this is via an example:
>>> a,*b,c=range(5)
>>> a
0
>>> b
[1, 2, 3]
>>> c
4
We've already seen a use for this — turning an iterable into a list in Python 3:
a=list(range(10))
*a,=range(10)
Here are a few more uses.
Getting the last element from a list
a=L[-1]
*_,a=L
In some situations, this can also be used for getting the first element to save on parens:
a=(L+[1])[0]
a,*_=L+[1]
Assigning an empty list and other variables
a=1;b=2;c=[]
a,b,*c=1,2
Removing the first of last element of a non-empty list
_,*L=L
*L,_=L
These are shorter than the alternatives L=L[1:] and L.pop(). The result can also be saved to a different list.
Tips courtesy of @grc