Yes, the only technical difference is that tuples are immutable. For me that's enough of a raison d'etre for tuples. Indexing dictionaries by a combination of keys is quite useful. Sets of tuples even more. I actually used that yesterday.
The other difference is more subtle. When you write 'return x,y' you think of your function as returning two values, not a single value that is a sequence of length 2. And when you type 'a, b = b, a' you think of swapping values, not packing and unpacking of sequences. Not having to type parentheses in most of these cases helps keep this illusion and I find that it makes python source code more beautiful. Beauty matters. Source code is a form of communication with other human beings. When you use a tuple, the fixed length carries the implicit connotation that each position has a specific role "first is x coordinate, second is y". "first is name, second is value", etc. When your reader sees the square brackets, you are signalling that "this is a sequence of essentially uniform items. Specific positions do not carry specific meanings although order might matter. The length of this list is expected to change between subsequent executions of the same piece of code or even during the lifetime of the specific list object". Oren On Wed, Jun 11, 2008 at 3:28 PM, Omer Zak <[EMAIL PROTECTED]> wrote: > > I have a silly question: > Why does Python have both Tuples and Lists? > > The only programmer-visible difference, of which I am aware, between > them is that Tuples are immutable and Lists are mutable, with the > following implications: > 1. Tuples can be used as hash (Dict) keys, and Lists cannot. > 2. When you modify a Tuple, a new Tuple is created. On the other hand, > List modification happens in place. > > I don't see why is the above difference good enough reason to have > different notations for Tuples and Lists. > --- Omer > -- > Every good master plan involves building a time machine. Moshe Zadka > My own blog is at http://www.zak.co.il/tddpirate/ > > My opinions, as expressed in this E-mail message, are mine alone. > They do not represent the official policy of any organization with which > I may be affiliated in any way. > WARNING TO SPAMMERS: at http://www.zak.co.il/spamwarning.html > > _______________________________________________ > Python-il mailing list > [email protected] > http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il _______________________________________________ Python-il mailing list [email protected] http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il
