[a,b,c,d] = 1,2,3,4

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Aug 25 16:50:41 CEST 2015


----- Original Message -----
> From: "ast" <nomail at invalid.com>
> To: python-list at python.org
> Sent: Tuesday, 25 August, 2015 4:16:17 PM
> Subject: [a,b,c,d] = 1,2,3,4
> 
> >>> [a,b,c,d] = 1,2,3,4
> >>> a
> 1
> >>> b
> 2
> >>> c
> 3
> >>> d
> 4
> 
> I have never seen this syntax before. Is it documented.
> Is there a name for that ?
> 
> thx

You probably have already seen something like:

a,b,c,d = 1,2,3,4

which is the same code than yours with the list replaced by a tuple.

Moreover:
https://docs.python.org/2/tutorial/datastructures.html

"""
x, y, z = t
This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires the list of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.
"""

It's slightly confusing because it mentions a "list of variable" and then a "tuple packing" while the example uses a tuple.
Fortunately, lists and tuples can be used in both cases.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


More information about the Python-list mailing list