My name is Paul Bissex, and e-scribe.com is my consulting business. I build web applications using as much open source software as possible. From September to June I teach web design and other important non-photographic professional skills to photographers. In the '90s I wrote technology commentary and reviews for magazines, newspapers, and web publications, including Wired, Salon.com, FamilyPC, the late lamented Web Review, and the Chicago Tribune. Feel free to email me.
I'm co-authoring a book, "Python Web Development with Django", with Jeff Forcier and Wesley Chun. It will be published by Prentice Hall in July 2008, but is available for pre-ordering on Amazon now.
This site is built on a fresh trunk checkout of Django, running on Python 2.5.1, served by Apache and mod_python. The database is SQLite. The operating system is FreeBSD, on a VPS hosted at Johncompanies.com. Comment-spam protection by Akismet. Vintage topo imagery from the Maptech archive.
Akismet, del.icio.us, Django, dpaste.com, Emacs, FreeBSD, Freenode, jQuery, LaunchBar, MacPorts, Markdown, Mercurial, OS X, Postfix, Python, SQLite, Subversion, TextMate, Trac, Ubuntu Linux, wmii
Copyright 2008
by Paul Bissex
and E-Scribe New Media
Python has two seemingly similar sequence types, tuples and lists, whose differences are addressed briefly in the Python FAQ under the heading "Why are there separate tuple and list data types?".
The difference between the two that people notice right away, besides literal syntax (parentheses vs. square brackets), is that tuples are immutable and lists are mutable. Unfortunately, because this distinction is strictly enforced by the Python runtime, some other more interesting differences in application tend to get overshadowed.
One common summary of these more interesting, if subtle, differences is that tuples are heterogeneous and lists are homogeneous. In other words:
What are these "kinds of stuff" I'm talking about? Types? Sometimes. But types may not tell the whole story.
Consider the following two data structures:
>>> import time
>>> time.localtime()
(2008, 2, 5, 11, 55, 34, 1, 36, 0)
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The first one, a tuple, is a sequence in which position has semantic value. The first position is always a year. This tuple functions as a lightweight record or struct.
The second one, a list, is a sequence where we may care about order, but where the individual values are functionally equivalent.
It's easy to imagine adding or removing items from the second one without breaking code that handles it or creating something undefined; for the first one, not so much.
A great example of the complementary use of both types is the Python DB API's fetchmany() method, which returns the result of a query as a list of tuples. The result set as a whole is a list, because rows are functionally equivalent (homogeneous); the individual rows are tuples, because rows are coherent, record-like groupings of (heterogeneous) column data.
A side note: Many functional languages, including Haskell and OCaml, have a similar distinction that is strictly enforced. Studying one of these languages may help clarify what I'm saying here; it certainly clicked for me when I started playing with Haskell.
There is considerable overlap in the ways tuples and lists can be used, but the built-in capabilities of the two structures highlight some of the above distinctions. For example, tuples have no 'index' method for identifying the position at which a particular value is found.
I've found that keeping the above distinctions in mind has helped me in my Python programming. Hopefully this attempt at clarification will help someone else!
The other nice side effect of tuples is that since the are immutable, they can be used as keys in dictionaries.
When people ask me about this I usually start my explanation with this abstraction:
Use a list when you need a bucket to put stuff in; "How can I get all these bricks to the roof?"
Use a tuple when you need a bucket of stuff; "Hope you enjoy this fruit basket."
Sorry, this is wrong. Tuples and lists may be either heterogeneous or homogeneous. (1,2), (1,'b'), [1,2] and [1,'b'] are all valid as are things like [1,(2,[3])]. While it may be the case that some developers use tuples for heterogeneous sets and others use lists for homogeneous sets, it's misleading to say that's the difference between the two.
The difference really is that lists are mutable. You can change the values in a list, append to it and delete values in it. You can't do that to a tuple.
Vroo -- perhaps my use of the word "are" in my third paragraph made things sound too definitive. I'm not claiming to describe "the" difference between the two types; I address the obvious mutable/immutable distinction up front.
What I'm attempting to describe in this post is a common (and in my opinion valuable) convention that is easy to miss when one is first learning Python.
Comments use Markdown syntax. Your comment will not appear until approved, which may take a few hours or more. Spammers will be torpedoed.
The iPhone keyboard doesn't suck
Python one-liner of the day
7 comments
How not to advocate via Google Code
2 comments
99 problems
3 comments
The original Lego Star Wars
2 comments
Toolbot.com source code available on request
bruce
Neat Python hack: infix operators
5 days ago
David Reynolds
The original Lego Star Wars
13 days ago
Jason Davies
Python one-liner of the day
15 days ago
Paddy3118
Let's play a game: BASIC vs. Ruby vs. Python vs. PHP
15 days ago
kbob
Python one-liner of the day
17 days ago
Dan
Let's play a game: BASIC vs. Ruby vs. Python vs. PHP
17 days ago
Leo Petr
Python one-liner of the day
17 days ago
At least 21945 pieces of comment spam killed since January 12th. Thanks are mostly due to Akismet.
The best thing I heard about the distinction of tuples vs. lists is that tuples have identity.
You would use tuples to represent a point (x,y): that alone already gives away the big semantic difference between tuples and lists and helps clarify that they are not just read-only lists.
In Django they're often used to represent, inside a list, an (id, value) pair that you can use in a Select widget.
Have a look at this post and the comment by Jayson Vantuyl.
L.