list vs tuple
aleaxit at yahoo.com
Fri Mar 30 13:38:20 CEST 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:email@example.com]
> > > 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
> > > point of it?
> > Off the top of my head - A tuple is immutable, and therefore hashable,
> > 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:
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).
More information about the Python-list