[docs] [issue14840] Tutorial: Add a bit on the difference between tuples and lists
report at bugs.python.org
Sat May 19 16:48:55 CEST 2012
Ezio Melotti <ezio.melotti at gmail.com> added the comment:
I think I liked the first version more, possibly with a few minor changes:
> Though tuples may seem very similar to lists, their immutability
> makes them ideal for fundamentally different usage.
I would drop the 'very', and I'm not sure that it's the immutability that enables this "fundamentally different" uses.
> In typical usage, tuples are a heterogenous structure,
> whereas lists are a homogenous sequence.
Instead of "In typical usage" this could just be "Usually".
> This tends to mean that, in general, tuples are used
> as a cohesive unit while lists are used one member at a time.
This could even be dropped IMHO, or something could be said about index access (or attribute access in case of namedtuples) vs iteration.
Maybe something like this could work:
Though tuples may seem similar to lists, they are often used in different situations and for different purposes.
Tuples are immutable, and usually contain an heterogeneous sequence of elements that are accessed via tuple-unpacking or indexing (or by attribute in the case of namedtuples). [Sometimes tuples are also used as immutable lists.]
Lists are mutable, and their elements are usually homogeneous and are accessed by iterating on the list.
FWIW homogeneous tuples are ok too, but here "homogeneous" is just a special case of "heterogeneous". IMHO the main difference between lists and tuples is the way you access the elements (and homogeneous vs heterogeneous is just a side-effect of this); the fact that they are mutable or not is a secondary difference.
Python tracker <report at bugs.python.org>
More information about the docs