what seems to be the problem with the code
l = [1,2,4,5,6,7,8]
t = *l
i have read that *l
gives comma separated single values so the statement should be similar to t = 1,2,3,4,5,6,7
which is a tuple but following code gives the error
Traceback (most recent call last):
File "<main.py>", line 4
SyntaxError: can't use starred expression here
what seems to be the problem with the code
l = [1,2,4,5,6,7,8]
t = *l
i have read that *l
gives comma separated single values so the statement should be similar to t = 1,2,3,4,5,6,7
which is a tuple but following code gives the error
Traceback (most recent call last):
File "<main.py>", line 4
SyntaxError: can't use starred expression here
Share
Improve this question
edited Jan 17 at 18:47
wjandrea
33.3k10 gold badges69 silver badges98 bronze badges
asked Jan 17 at 18:21
Sachin DobhalSachin Dobhal
213 bronze badges
4
|
1 Answer
Reset to default 5PEP 448, the PEP that introduced the kind of unpacking you're trying to do, specifically allows unpacking "inside tuple, list, set, and dictionary displays".
If you want to unpack l
into a tuple on the RHS of an assignment, the unpacking has to happen in a "tuple display" - syntax that would have created a tuple even without the unpacking. l
would not create a tuple, so your unpacking is invalid.
You can make the unpacking work with code like
t = *l,
since t = l,
would be valid syntax to create a one-element tuple.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745349155a4623721.html
t = tuple(l)
– Barmar Commented Jan 17 at 18:52