[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Can tuples be replaced with lists all the time?

On 23/02/2014 17:48, Roy Smith wrote:
> In article <led9s7$req$1 at reader1.panix.com>,
>   Grant Edwards <invalid at invalid.invalid> wrote:
>> In constrast, tuples are often used as fixed-length heterogenous
>> containers (more like a struct in C except the fields are named 0, 1,
>> 2, 3, etc.).  In a particular context, the Nth element of a tuple will
>> always mean one thing (e.g. a person's last name) while the Mth
>> element will always be something else (e.g. a person's age).
> And, of course, namedtuples make that much more explicit.
> It also appears that tuples are more memory efficient.  I just ran some
> quick tests on my OSX box.  Creating a list of 10 million [1, 2, 3, 4,
> 5] lists gave me a 1445 MB process.   The name number of (1, 2, 3, 4, 5)
> tuples was 748 MB.  I'm sure this is implementation dependent, but it
> seems plausible to assume similar results will be had on other
> implementations.

In CPython a list is overallocated so there's usually spare slots 
available if you want to add something to it.  In contrast you know when 
you create the tuple just how big it is so no overallocation is needed.

My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

This email is free from viruses and malware because avast! Antivirus protection is active.