list vs tuple
Alex Martelli
aleaxit at yahoo.com
Fri Mar 30 06:38:20 EST 2001
"EuphoriaDj" <eddthompson at ssi.nospamhere.parlorcity.com> wrote in message
news:3ac40acd$0$10576 at wodc7nh0.news.uu.net...
>
> "Simon Brunning" <SBrunning at trisystems.co.uk> wrote in message
> news:mailman.985789357.19622.python-list at python.org...
> > > From: deadmeat [SMTP:root@[127.0.0.1]]
> > > Whats the practical difference between a list[] and a tuple() ?
> > >
> > > A tuple seems to be a very basic list, so unless it's faster, what's
the
> > > point of it?
> >
> > Off the top of my head - A tuple is immutable, and therefore hashable,
and
> > therefore can be used as a key to a dictionary. I'm sure that there are
> > other things...
> >
> what do you mean by hashable?
Uh, what about "able to be hashed", where "hashed" is the
past participle of verb "to hash"? Python has a builtin
function "hash", which accepts only "hashable" objects,
and returns an integer:
>>> hash((1,2,3))
-821448277
>>> hash([1,2,3])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unhashable type
>>>
As you see, you can hash a tuple (a tuple is hashable) but
not a list (it's unhashable). As Simon goes on to say,
being hashable is equivalent to being usable as a key to
a dictionary (since the dictionary internally uses hash
on its keys).
Alex
More information about the Python-list
mailing list