Tuple vs List: Whats the difference?

S

Shafik

Hello folks,

I am an experienced programmer, but very new to python (2 days). I
wanted to ask: what exactly is the difference between a tuple and a
list? I'm sure there are some, but I can't seem to find a situation
where I can use one but not the other.

Thanks in advance,
--Shafik
 
A

Alexander Schmolck

Shafik said:
Hello folks,

I am an experienced programmer, but very new to python (2 days). I
wanted to ask: what exactly is the difference between a tuple and a
list? I'm sure there are some, but I can't seem to find a situation
where I can use one but not the other.

Try mutating the tuple or using the list as a hashkey.

'as
 
D

Dave Baum

Shafik said:
Hello folks,

I am an experienced programmer, but very new to python (2 days). I
wanted to ask: what exactly is the difference between a tuple and a
list? I'm sure there are some, but I can't seem to find a situation
where I can use one but not the other.

Lists and tuples are both sequences, and can be used interchangeably in
many situations. The big difference is that lists are mutable (can be
modified), and tuples are immutable (cannot be modified). Lists also
have a richer API (for example the index and count methods).

If you are going to need to change the contents of the sequence
(appending, removing, altering a value, etc), then use a list.

If you require the sequence to be immutable (such as being used as the
key in a dictionary), then use a tuple.

In general, tuples should be a little more efficient than lists.
However, altering a list is more efficient than creating a new tuple, so
"always prefer tuples" does not necessarily lead to a faster overall
program. Unless performance is critical, I wouldn't worry about this
factor.

IMHO, a more important reason to prefer tuples is that since they are
immutable, you don't have to worry about side effects. If you call a
function and pass it a list, you have no guarantee that the list won't
be changed during the function call. With tuples, any attempt to change
the tuple will raise an exception. If it is important to the caller
that the sequence remain unchanged, then a tuple is a safer
representation for that sequence.

Dave
 
H

Hendrik van Rooyen

Hello folks,

I am an experienced programmer, but very new to python (2 days). I
wanted to ask: what exactly is the difference between a tuple and a
list? I'm sure there are some, but I can't seem to find a situation
where I can use one but not the other.

Welcome to Python.
From a practical point of view, use lists - you will get exceptions
if you use a list where a tuple is required, such as a key in a dict,
and other more obscure cases.

The philosophical differences - "tuple as struct" vs "list as collection"
argument, makes no practical difference - you can use a list in the same
structured way if you want, and lists have more nice methods as they
are not immutable as tuples are.

Google this Group for "Index" to see some flame wars.

hth - Hendrik
 
B

Ben Finney

Dave Baum said:
Lists and tuples are both sequences, and can be used interchangeably
in many situations. The big difference is that lists are mutable
(can be modified), and tuples are immutable (cannot be modified).
Lists also have a richer API (for example the index and count
methods).

This common misconception falls foul of only considering the
implementation details.

The choice of when to use one or the other is more one of intent: A
tuple is best for heterogeneous sequences, a list is best for
homogeneous sequences. Here's a better explanation:

<URL:http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists>
 
S

Shafik

Thanks to all of you, Im sure I'll pick your brains some more later
down the line.

--Shafik
 

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
474,298
Messages
2,571,539
Members
48,273
Latest member
latemarriage

Latest Threads

Top