[docs] [issue14840] Tutorial: Add a bit on the difference between tuples and lists

Zachary Ware report at bugs.python.org
Fri May 18 23:59:30 CEST 2012

Zachary Ware <zachary.ware at gmail.com> added the comment:

I'll go with foolhardy, or just "green" :P. I wasn't aware that this topic was quite as contentious as it seems to be.

I agree that tuples and lists are similar.  I was trying to keep my wording at "here's another way to look at things that you may not have thought of before" without discrediting other ways of thinking, but looking at it again today, there is definite room for improvement on that front.  As for "fundamentally different," my intent was to condense "tuples can be used in a way that lists don't fit as well in, and that usage is much different than lists' usual usage" into as few words as I could; referring to the usages rather than the objects being fundamentally different.

I'm guessing your main issue with the second sentence is "typical usage."  I agree, that really isn't the best choice of words, especially when trying to stay away from "this is how to do it, don't deviate."

I didn't leave out other tuples uses intentionally, but my purpose was really to point out another way of thinking.  Not long ago, I was wondering what the difference between tuples and lists really was and went searching.  When I found something about "heterogenous structure vs. homogenous sequence," it was like a light bulb turning on in my head.  I hadn't seen anything like that anywhere in the tutorial or docs before, and it made a lot of things make more sense.  Your dis() examples have done the same kind of thing for me, to a lesser extent; I'd never realized just how much less work it is for the interpreter to create a tuple than a list.  It's my belief that the tutorial should pack as many "ah ha!" moments as it can into as little space as it can.

So, here's another stab:

It may seem that tuples are very similar to lists (and they are in many ways),
but their immutability makes them ideal for some cases that lists don't fit
quite as well.  Though hetero- or homogeneity is in no way a programmed property
of anything in Python's syntax or the standard library, it can be helpful to
think of tuples as heterogenous structures, and lists as homogenous sequences.
When used in this way, tuples are used as a coherent unit while lists are used
one member at a time.  This is not to say tuples can only be used as a
heterogenous structure.  In fact, there are parts of Python's own syntax that
require a tuple that happens to be a homogenous sequence, such as the
:keyword:`except` clause of a ``try ... except`` statement, :func:`issubclass`,
and :func:`isinstance`.

Added file: http://bugs.python.org/file25633/tuple vs list.patch.2

Python tracker <report at bugs.python.org>

More information about the docs mailing list