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  
Old 07-16-2007, 10:14 PM
vadivelanvaidyanathan vadivelanvaidyanathan is offline
D-Web Genius
 
Join Date: Feb 2007
Posts: 801
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
  #2  
Old 07-30-2007, 03:30 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 881
kingmaker is on a distinguished road
Send a message via MSN 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.
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 Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Best Bookmarking list rosemktg Promotion Techniques 0 07-13-2009 06:41 PM
List out all the similarities/difference between an Abstract class and Interface? H2o ASP and ASP.NET Programming 1 07-24-2007 02:16 AM
List Difference between client server testing and web testing itbarota Software Testing 1 07-23-2007 10:26 PM
list box nssukumar Flash Actionscript Programming 0 07-10-2007 05:34 AM
Does anyone else have a Life To Do List? cocowgirl29 The Lounge 5 04-05-2007 05:58 AM


All times are GMT -7. The time now is 10:15 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0