IT Community - Software Programming, Web Development and Technical Support

What is the difference between tuple and list?

This is a discussion on What is the difference between tuple and list? within the Python forums, part of the Software Development category; What is the difference between tuple and list?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Python

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-16-2007, 11:14 PM
vadivelanvaidyanathan vadivelanvaidyanathan is offline
D-Web Genius
 
Join Date: Feb 2007
Posts: 803
vadivelanvaidyanathan is on a distinguished road
Question What is the difference between tuple and list?

What is the difference between tuple and list?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-30-2007, 04:30 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: What is the difference between tuple and list?

Hi Vadi,

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.
__________________
The KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
List out all the similarities/difference between an Abstract class and Interface? H2o ASP and ASP.NET Programming 1 07-24-2007 03:16 AM
List Difference between client server testing and web testing itbarota Software Testing 1 07-23-2007 11:26 PM
list box nssukumar Flash Actionscript Programming 0 07-10-2007 06:34 AM
List in Google in Just 7 days vadivelanvaidyanathan Google 3 04-26-2007 01:21 PM
Does anyone else have a Life To Do List? cocowgirl29 The Lounge 5 04-05-2007 06:58 AM


All times are GMT -7. The time now is 03:02 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0