4

I am learning python and I have as simple list as this

z = [1,2,3,4,5,6,7,8,9]

and I am just trying to unpack it, which throws me syntax error.

a,*b,c = z
>>> a,*b,c = z
  File "<stdin>", line 1
    a,*b,c = z
      ^
SyntaxError: invalid syntax

I tried changing the order of variable as well but same error. Any suggestion pls.

5
  • 1
    *b is not a valid name for a variable Commented Jan 14, 2016 at 1:12
  • What were you try to get with *b? Were you trying to separate the first and last element from the middle of the list? Commented Jan 14, 2016 at 1:14
  • 5
    Extended unpacking is only implemented/possible in python 3 - your question is tagged as python 2.7 and 3.x. What version are you using? Commented Jan 14, 2016 at 1:14
  • Ref python.org/dev/peps/pep-3132 and docs.python.org/3/whatsnew/3.0.html#new-syntax Commented Jan 14, 2016 at 1:15
  • @TomDalton I am using 2.7 as mentioned in title of question but I guessed that it might be due to version so I want to reach out wider audience for their suggestion Commented Jan 14, 2016 at 1:16

2 Answers 2

3

What your are trying to do does not work in Python2 because it does not have support for extended tuple unpacking - you pretty much have to use slicing.

>>> z = [1,2,3,4,5,6,7,8,9]
>>> a, b, c = z[0], z[1:-1], z[-1]
>>> a
1
>>> b
[2, 3, 4, 5, 6, 7, 8]
>>> c
9

Using Python3, you are allowed one wildcard:

Demo:

>>> z = [1,2,3,4,5,6,7,8,9]
>>> a, *b, c = z
>>> a
1
>>> b
[2, 3, 4, 5, 6, 7, 8]
>>> c
9
>>> *a, b, c = z
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>> b
8
>>> c
9
>>> *a, *b, c = z
  File "<stdin>", line 1
SyntaxError: two starred expressions in assignment

Python3.5 comes with some additional unpacking generalizations

>>> [1, 2, *[3, 4]]
[1, 2, 3, 4]
Sign up to request clarification or add additional context in comments.

2 Comments

And it's a damn shame that the new generalizations don't work in comprehensions, or we could write [*sublist for sublist in nested_list] in 3.5 to flatten a list of lists.
Eh. Works with generator expressions, so: [*itertools.chain(*nested_list)] works. Not too bad; shortens if you import chain into global namespace directly.
3

Here is a quote from PEP-3132

For example, if seq is a slicable sequence, all the following assignments are equivalent if seq has at least three elements:

a, b, c = seq[0], list(seq[1:-1]), seq[-1]
a, *b, c = seq
[a, *b, c] = seq

In Python2.7, only the first version is legal syntax.

Since you already know z is a list, you can just write

a, b, c = z[0], z[1:-1], z[-1]

This will work for Python2.7 and Python3.x

2 Comments

If seq is a generator this won't work, better to do lst = list(seq) then slice lst.
A generator is neither slicable nor a sequence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.