472,954 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

Tuple vs List: Whats the difference?

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
7 4395
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
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
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
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
"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
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
Thanks to all of you, Im sure I'll pick your brains some more later
down the line.

--Shafik
Jul 12 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Yomanium Yoth Taripoät II | last post by:
HI, 1) what are the differences between list and tuple? 2) how to concatenate tuple and list? no method, no opérator? 3) im looking the fucking manual, and cant add value in my tuple, when it...
2
by: Ishwar Rattan | last post by:
I am a little confused about a list and a tuple. Both can have dissimilar data-type elements, can be returned by functions. The only difference that I see is that list is mutable and tuple is...
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
29
by: George Sakkis | last post by:
Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that tuples are immutable ? I ended up writing a custom ImmutableSequence class that does this, but I...
5
by: Dave Opstad | last post by:
I'm hoping someone can point out where I'm going wrong here. Here's a snippet of a Python interactive session (2.3, if it makes a difference): -------------------------------------- >>> class...
7
by: daniel | last post by:
is there any typical usage that shows their difference? thanks daniel
77
by: Nick Maclaren | last post by:
Why doesn't the tuple type have an index method? It seems such a bizarre restriction that there must be some reason for it. Yes, I know it's a fairly rare requirement. Regards, Nick...
5
by: JimDibb | last post by:
Please don't critique my algorithm as I'm a rank newbie (or go ahead, I can take it :) ). But I can't figure the following out. def shuffle(): """ Return a list of indices from 0-51 with...
10
by: TP | last post by:
Hi everybody, I have a question about the difference of behavior of "len" when applied on tuples or on lists. I mean: $ len( ( 'foo', 'bar' ) ) 2 $ len( ( 'foo' ) ) 3 $ len( )
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.