By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 2,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

Tuple vs List: Whats the difference?

P: n/a
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

Jul 11 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 7/11/07, Shafik <sh******@gmail.comwrote:
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.
The differences are explained pretty good on here. Although, I am
pretty new to Python as well.

http://diveintopython.org/native_data_types/tuples.html
Jul 11 '07 #2

P: n/a
Shafik <sh******@gmail.comwrites:
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
Jul 11 '07 #3

P: n/a
On Jul 11, 12:05 pm, Alexander Schmolck <a.schmo...@gmail.comwrote:
Shafik <shafi...@gmail.comwrites:
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
Cool thanks guys.

--Shafik
Jul 11 '07 #4

P: n/a
In article <11**********************@22g2000hsm.googlegroups. com>,
Shafik <sh******@gmail.comwrote:
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
Jul 12 '07 #5

P: n/a
"Shafik" <s...3@gmail.comwrote:

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

Jul 12 '07 #6

P: n/a
Dave Baum <Da*******@motorola.comwrites:
In article <11**********************@22g2000hsm.googlegroups. com>,
Shafik <sh******@gmail.comwrote:
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).
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>

--
\ "No matter how cynical I get, I can't seem to keep up." -- |
`\ Lily Tomlin |
_o__) |
Ben Finney
Jul 12 '07 #7

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

--Shafik
Jul 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.