I'm Paul Bissex, and e-scribe.com is my consulting business. I build web applications using open source software, especially Django. I teach photographers web design and professional skills. In the '90s I did graphic design for newspapers and magazines. Then I wrote technology commentary and reviews for Wired, Salon.com, Chicago Tribune, and lots of little places you've never heard of. Feel free to email me.
I'm co-author of "Python Web Development with Django", an excellent guide to my favorite web framework. Its strong points include an introduction to Python, and better coverage of Django 1.0 than nearly anybody else. Published by Addison-Wesley, it is available from Amazon and your favorite technical bookstore as well.
Built using Django, served by Apache and mod_wsgi. 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. The markup engine is Markdown.
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
At least 67519 pieces of comment spam killed since January 2008, mostly via Akismet.
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.
I think I'm starting to grasp the idea of tuples and lists. This might be a stupid question (I'm a newbie programmer), but I'm a bit confused as to in what conditions would you choose to use tuple over a list (since tuples are immutable, lists sound to be more useful maybe?)
Also, you mentioned that tuples can be more efficient, can you give a case for this?
Thanks!
Thanks for reading! Please note: Your comment will not appear until approved, which may take a few hours or more. Spammers will be torpedoed.
Branching and merging in real life
7 comments
Summer Spam
1 comment
SPF-enabled spam domains
1 comment
Chess via iPod
2 comments
Aesthetics and computation
2 comments
Brett Spurrier
Software for determining image similarity?
19 days ago
nizamfarooq
eBay, fraud, filtering, and Web 2.0
55 days ago
Derek
World's ugliest Django app
86 days ago
sagar
Sort tables with sorttable.js
105 days ago
Paintball Kolbudy
Summer Spam
112 days ago
Copyright 2010
by Paul Bissex
and E-Scribe New Media
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][1] and the comment by Jayson Vantuyl.
L.
[1]: http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/