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

James Stroud jstroud at mbi.ucla.edu
Fri Apr 20 19:18:28 EDT 2007


garrickp at gmail.com wrote:
> 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

You can also do a list swap.

py> a = 4
py> b = 2
py> [a, b] = [b, a]
py> a
2
py> b
4



More information about the Python-list mailing list