[Tutor] tuples versus lists
Michael P. Reilly
arcege at gmail.com
Fri Sep 15 04:59:28 CEST 2006
Tuples, like other immutable data types, are hashable (assuming the contents
are immutable as well), and so they can be used as keys to dictionaries.
Lists cannot be used this way.
>>> {['a']: 'a'}
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list objects are unhashable
>>> {('a',): 'a'}
{('a',): 'a'}
>>>
-Arcege
On 9/14/06, Tim Johnson <tim at johnsons-web.com> wrote:
>
> * Kermit Rose <kermit at polaris.net> [060914 18:29]:
> > Hello Brian.
> >
> > Today I read through chapter five of the python tutorial and discovered
> > that tuples are not the same thing as lists.
> >
> > This surprised me.
> >
> > I do not see any difference in the capability of lists and tuples.
>
> Hi Kermit:
>
> Tuples are "read-only" - you can't modify a tuple,
>
> you can produce a tuple from a list by using the
> tuple() function.
> you can produce a list from a tuple by using the
> list() function - but the original tuple is unchanged.
>
> You can return multiple values from a function
> using tuples.
> >
> > Why would you use one in preference to the other?
>
> I use tuples as above and where I don't want data
> changed...
>
> tim
> --
> Tim Johnson <tim at johnsons-web.com>
> http://www.alaska-internet-solutions.com
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060914/ce77ee81/attachment.html
More information about the Tutor
mailing list