Can tuples be replaced with lists all the time?
On 2014-02-23, 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
In addition to the things related to one being mutable and the other
immutable, there is a certain tradition that often results in differnt
usages for them.
Though it's certainly not required, lists are more often used as
variable length _homogeneous_ containers -- sort of like a variable
length array or linked list in C. The "type" or "meaning" of element
#N is the same as that of element #M.
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).