suggestions for using tuples instead of list (or vice versa)

T

Thorsten Kampe

I found out that I am rarely using tuples and almost always lists
because of the more flexible usability of lists (methods, etc.)

To my knowledge, the only fundamental difference between tuples and
lists is that tuples are immutable, so if this is correct, than list
are a superset of tuples, meaning lists can do everything tuples can
do and more.

Is there any advantage for using tuples? Are they "faster"? Consume
less memory? When is it better to use tuples instead of lists and when
better to use lists instead of tuples?

Thorsten
 
D

Diez B. Roggisch

Is there any advantage for using tuples? Are they "faster"? Consume
less memory? When is it better to use tuples instead of lists and when
better to use lists instead of tuples?

AFAIK tuples are faster and less memory consuming. I personally prefer them
for e.g. returning multiple values from a function or when I know I won't
need the power of a list. But its largely considered a matter of taste.
Google this newsgroup for a plethora of discussions on this subject....
 
P

Peter Hansen

Thorsten said:
I found out that I am rarely using tuples and almost always lists
because of the more flexible usability of lists (methods, etc.)

To my knowledge, the only fundamental difference between tuples and
lists is that tuples are immutable, so if this is correct, than list
are a superset of tuples, meaning lists can do everything tuples can
do and more.

Only tuples can be used as dictionary keys.
Is there any advantage for using tuples? Are they "faster"? Consume
less memory?

Not appreciably so in most cases. Certainly not enough to
be a factor in choosing which to use except perhaps in
very rare edge cases.
> When is it better to use tuples instead of lists and when
better to use lists instead of tuples?

The canonical answer (which some disagree with) is that you
should use tuples when you have a collection of different
types of items, such as a 'struct' in C would have. Use
lists any other time you want a simple sequential collection
of items.

Use a list for effectively everything else, unless you need
an immutable (tuple) for a dictionary key.

One description of how to look at this that I've found helpful
is something along the lines of "if you can take a slice of
the data and consider it in the same way as the original,
then you should use a list". For example, if you have a set
of fifteen objects over which you plan to iterate, you can
just as well iterate over the first five. If, however, you
have a tuple of (year, month, day, hour, minute, second),
taking the first four items is quite a different thing than
taking all six of them.

-Peter
 
R

Roy Smith

Thorsten Kampe said:
I found out that I am rarely using tuples and almost always lists
because of the more flexible usability of lists (methods, etc.)

There was an extensive thread on this topic within the last month or
two. Like many threads, it rambled and wandered a bit, but covered the
topic quite well. DAGS for list or tuple in the subject, date <= 2
months, and you should find it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top