List vs tuples
John Roth
newsgroups at jhrothjr.com
Thu Apr 8 16:57:25 EDT 2004
"Mitja" <tezt at email.si> wrote in message
news:c54cjb$5fs$1 at planja.arnes.si...
> A neewbie's question here, probably discussed before:
> what's the purpose of having both lists AND tuples?
> At the first (and second and third, to be honest) glance lists are just
> tuples with extended functionality, so why have tuples around?
> I've been working with Python for a few months now but haven't yet come
> across a problem that would really require tuples.
It's a common question. A list is a data structure that is
intended to be used with homogenous members; that is,
with members of the same type.
You've probably used tuples without realizing it. For
example, the following idiom involves a tuple:
hours, minute, second = getTime()
where
def getTime(.....)
#some calc
return hour, minute, second
While it looks like return is returning multiple
objects, it actually isn't. It's returning one object:
a tuple of three elements which is then being
unpacked on the receiving end.
You could call the same routine this way as well:
result = getTime()
and result would be a tuple of three elements.
Another way of thinking about it is that you would
use a tuple the same way you would use a struct in
a language like C, if you didn't want to go to the trouble
of creating a full-fledged object for it.
John Roth
>
> Thanks in advance,
> Mitja
>
>
More information about the Python-list
mailing list