list assignment

N

Norvell Spearman

In "Learning Python," by Lutz and Ascher, there's a table showing different
assignment statement forms. One form shown is list assignment. The authors
give this as an example:

[spam, ham] = ['yum', 'YUM']

I don't see how this is any different than a tuple unpacking assignment:
>>> a, b = 1, 2
>>> a, b (1, 2)
>>> [a, b] = [1, 2]
>>> a, b
(1, 2)

In both instances the names a and b are both mapped to 1 and 2 so why are there
two different forms?

Thanks for any answers.
 
R

Raymond Hettinger

[spam, ham] = ['yum', 'YUM']
I don't see how this is any different than a tuple unpacking assignment:

It's not different. They are ways of writing the same thing.


Raymond Hettinger
 
N

Norvell Spearman

Raymond said:
It's not different. They are ways of writing the same thing.

Lutz and Ascher have tuple and list assignment as separate entries in their
assignment statement forms table so I was expecting there to be some
difference; thanks for setting me straight.
 
B

Bernhard Herzog

Norvell Spearman said:
Lutz and Ascher have tuple and list assignment as separate entries in
their assignment statement forms table so I was expecting there to be
some difference; thanks for setting me straight.

In older Python versions there was a difference between list unpacking
and tuple unpacking. The former would only work with lists and the
latter with tuples. With Python 1.5, both were unified into a more
general sequence unpacking, but for backwards compatibility both
syntaxes were kept.

Bernhard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top