Lists vs tuples (newbie)

S

Szabolcs

I was wondering about why are there both tuples and lists? Is there
anything I can do with a tuple that I cannot do with a list?

In what circumstances is it advantageous to use tuples instead of lists?
Is there a difference in performance?

I am still learning Python, so please be gentle ...

Szabolcs
 
D

Duncan Booth

Szabolcs said:
I was wondering about why are there both tuples and lists? Is there
anything I can do with a tuple that I cannot do with a list?

In what circumstances is it advantageous to use tuples instead of lists?
Is there a difference in performance?

I am still learning Python, so please be gentle ...
Have you found the Python FAQ yet? It answers this exact question and many
others:

http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types
 
R

rzed

I was wondering about why are there both tuples and lists? Is
there anything I can do with a tuple that I cannot do with a
list?

In what circumstances is it advantageous to use tuples instead
of lists? Is there a difference in performance?

I am still learning Python, so please be gentle ...
This topic comes up from time to time in this newsgroup. If you want
a lot of viewpoints about it, Google is your friend.

A short answer, though: tuples can be used as dictionary keys and
lists cannot. I would guess (but would have to test to confirm) that
tuples occupy less space for the same data. I don't know whether any
differences in, say, iteration speed would be terribly significant,
but I would expect tuples to be marginally faster.
 
P

Phoe6

In what circumstances is it advantageous to use tuples instead of lists?
Is there a difference in performance?

As you should not, tuples are immutable while lists are mutable. You
can think of all the scenarios where immutable datatype are required.

1) Return values from a function. When you return multiple values
from a function. You store them as a tuple and access them
individually rather then in the list, which bear the danger of being
modified.
Look up the standard library itself and you will find many instances.

(cin, cout, cerr) = os.popen3('man man')

If you had the above as list, then you might end up spoiling things
knowingly/unknowingly.

2) Programming requirements arise when you have to deal with
persistent data.


HTH.
Senthil

http://uthcode.sarovar.org
 
S

Szabolcs

Thanks for all the replies!
1) Return values from a function. When you return multiple values
from a function. You store them as a tuple and access them
individually rather then in the list, which bear the danger of being
modified.
Look up the standard library itself and you will find many instances.

(cin, cout, cerr) = os.popen3('man man')

If you had the above as list, then you might end up spoiling things
knowingly/unknowingly.

Could you please elaborate on this (or give an explicit example how
might one do something bad unknowingly when returning multiple values in
a list)?

Should I think of tuples simply as a safeguard and reminder (because I
consciously use them for different thing than lists, as the faq
suggests)? Something similar to C++'s "const" (i.e. not strictly
necessary but useful for avoiding bugs)?

Szabolcs
 
A

Alexander Schmolck

Szabolcs said:
Thanks for all the replies!


Could you please elaborate on this (or give an explicit example how might one
do something bad unknowingly when returning multiple values in a list)?


def f(input, defaultAnswer=[1,2,3]):
if input == 1: return (4,5,6)
else: return defaultAnswer

print f(0).pop(), f(0).pop(), f(0).pop(), f(0)


But that's a pretty pathological; just using unstructuring as in the open
example above nothing bad can happen.
Should I think of tuples simply as a safeguard and reminder (because I
consciously use them for different thing than lists, as the faq suggests)?

Use them for hetoerogeneous data or when you need the immutability (so that
people can't screw)

'as
 
H

Hendrik van Rooyen

Szabolcs said:
I was wondering about why are there both tuples and lists? Is there
anything I can do with a tuple that I cannot do with a list?

In what circumstances is it advantageous to use tuples instead of lists?
Is there a difference in performance?

I am still learning Python, so please be gentle ...

On this group you do not have to be afraid - there may be barking,
but the bulldogs all have rubber teeth...
From a practical perspective, use lists - they have more methods.
When you want to use a list as a key in a dict, then you can't,
as keys have to be immutable.

So then you convert your list to a tuple and use it as a key.
Its the only place I have found where you *have* to use a tuple.

You will be told a lot of stuff about lists being for homogenous
items, and tuples being records like a C struct. Take note, nod
your head, and then ignore it, and put whatever you like into your
lists. They are your lists, not someone else's, and you can put in
just what you like, and it all works, in spite of whatever the design
intention had been.

Aside from the hashing issue, there is nothing that a tuple can do
that can't be done as well or better by a list.

Heretically yours,

Hendrik
 
D

Duncan Booth

Hendrik van Rooyen said:
Aside from the hashing issue, there is nothing that a tuple can do
that can't be done as well or better by a list.

There are a few other cases where you have to use a tuple, for example in a
try..except statement the exception specification must be an exception to
be caught or a tuple of exception specifications: a list won't work to
catch multiple exceptions.
 
H

Hendrik van Rooyen

There are a few other cases where you have to use a tuple, for example in a
try..except statement the exception specification must be an exception to
be caught or a tuple of exception specifications: a list won't work to
catch multiple exceptions.

Esoteric - But I stand corrected...

any other "must use a tuple's " ?

Seems they all fall into a class that can be described as required by the
language - I was thinking data.

- Hendrik
 
N

Neil Cerutti

There are a few other cases where you have to use a tuple, for
example in a try..except statement the exception specification
must be an exception to be caught or a tuple of exception
specifications: a list won't work to catch multiple exceptions.

I use tuples simply because of their mellifluous appellation.
 
D

Duncan Booth

Hendrik van Rooyen said:
Esoteric - But I stand corrected...

any other "must use a tuple's " ?

Seems they all fall into a class that can be described as required by
the language - I was thinking data.
There used to be several other situations where a tuple was required but I
can't think of any others which still exist.

So far as data is concerned, the same reason that exceptions require tuples
could arise in your own code:

If you look at the definition of the except clause expression (simplified
to remove the deprecated string option):
An object is compatible with an exception if it is the class or a base
class of the exception object or a tuple containing an item compatible
with the exception

you'll see that it is impossible for this structure to contain any loops.
An equivalent structure using lists instead of tuples could contain loops
and any code which searched it for a match would have to account for this
somehow.

So, anywhere you want a tree structure with a guarantee that it does not
contain any loops it may be appropriate to use tuples to represent the
structure.
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top