Can tuples be replaced with lists all the time?

Roy Smith roy at panix.com
Sun Feb 23 05:19:22 CET 2014


In article <64af70e3-6876-4fbf-8386-330d2f48735a at googlegroups.com>,
 Sam <lightaiyee at gmail.com> wrote:

> My understanding of Python tuples is that they are like immutable lists. If 
> this is the cause, why can't we replace tuples with lists all the time (just 
> don't reassign the lists)? Correct me if I am wrong.

There are many tasks for which either a list or a tuple would work fine.  
But, not all.  Basically:

If you need something which is hashable (i.e. to be used as a dictionary 
key), you need to use a tuple.

If you need something which you can mutate (say, to accumulate items as 
you go through a loop), then you need to use a list.

Beyond that, you get into religious territory.



More information about the Python-list mailing list