473,503 Members | 7,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4425
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
4060
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
2072
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
3233
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
3365
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
1440
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
5788
by: daniel | last post by:
is there any typical usage that shows their difference? thanks daniel
77
4353
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
2347
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
1494
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
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.