what is the difference between the two kinds of brackets?

Steve Lamb grey at dmiyu.org
Sat Oct 20 14:43:31 CEST 2007

```On 2007-10-20, James Stroud <jstroud at mbi.ucla.edu> wrote:
><commentary>The long of it is that there are deep computer-science
> issues that distinguish the two and the differences become more
> important the more you know (presumably). However, I have been
> programming this language for 5 years, and I still can't figure out the
> necessity for having both. I have heard all of the arguments, however
> but none are terribly pursuasive.</commentary>

The quick answer is that tuples can be indexes into directories while
lists cannot.  This doesn't seem like that big of a deal until you realize it
allows you to group related but distict bits of data together to form an index
while retaining their individual identity.  A simplistic example would be if
you had a map with x,y coordinates and you wanted to place items on that map
at their location.

grey at mania:~} python
Python 2.4.4 (#2, Aug 16 2007, 02:03:40)
[GCC 4.1.3 20070812 (prerelease) (Debian 4.1.2-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 1
>>> y = 1
>>> coord = (x, y)
>>> map = {}
>>> map[coord] = "Something at x%s, y%s" % coord
>>> map
{(1, 1): 'Something at x1, y1'}
>>> map[(1,1)]
'Something at x1, y1'
>>> if map.has_key(coord):
...     print "Found it!"
...
>>> if map.has_key((1,1)):
...     print "Found it!"
...
Found it!
>>> for loc in map:
...     print "X: %s, Y: %s - %s" % (loc[0], loc[1], map[loc])
...
X: 1, Y: 1 - Something at x1, y1

The lists cannot be indexes to directories because they are mutable.
[1. 1] might not be [1, 1] the next time you look for it.  (1, 1) always will.

--
Steve C. Lamb         | But who decides what they dream?
PGP Key: 1FC01004       |   And dream I do...
-------------------------------+---------------------------------------------

```

More information about the Python-list mailing list