When are immutable tuples *essential*? Why can't you just use lists *everywhere* instead?

garrickp at gmail.com garrickp at gmail.com
Fri Apr 20 18:53:45 EDT 2007


On Apr 20, 4:37 pm, John Machin <sjmac... at lexicon.net> wrote:
> One inessential but very useful thing about tuples when you have a lot
> of them is that they are allocated the minimum possible amount of
> memory. OTOH lists are created with some slack so that appending etc
> can avoid taking quadratic time.

Speaking of inessential but very useful things, I'm also a big fan of
the tuple swap...
a = 2
b = 3
(a, b) = (b, a)
print a # 3
print b # 2

As well as the simple return of multiple values from a single
function:

c_stdout, c_stdin = popen2("ls")

IMO, the biggest thing going for tuples is the syntactical sugar they
bring to Python. Doing either of these using lists or other data
constructs would not be nearly as clean as they are with tuples.




More information about the Python-list mailing list