Results 1 to 3 of 3

Thread: Python tuple vs list - what is the differences between the two data types?

  1. #1
    Contributors chimu's Avatar
    Join Date
    Mar 2005
    Posts
    92
    Thanks
    28
    Thanked 5 Times in 4 Posts
    Rep Power
    9

    Default Python tuple vs list - what is the differences between the two data types?

    What's the difference between list and tuples in Python? Which one is recommended ?
    Last edited by chimu; 25th November 2012 at 03:14 PM.

  2. #2
    Senior Member Rahul.Patil's Avatar
    Join Date
    Feb 2012
    Location
    Mumbai india
    Posts
    447
    Thanks
    10
    Thanked 46 Times in 43 Posts
    Rep Power
    6

    Default

    Hi,

    Please refer this page for the same. read answer which have Green tick.

    Which one is recommended ?
    >> depend on situations, if you want fixed values then use tuples

    some more info copied from this page
    Python has two seemingly similar sequence types, tuples and lists.

    The difference between the two that people notice right away, besides literal syntax (parentheses vs. square brackets), is that tuples are immutable and lists are mutable. Unfortunately, because this distinction is strictly enforced by the Python runtime, some other more interesting differences in application tend to get overshadowed.

    One common summary of these more interesting, if subtle, differences is that tuples are heterogeneous and lists are homogeneous. In other words:

    Tuples (generally) are sequences of different kinds of stuff, and you deal with the tuple as a coherent unit.
    Lists (generally) are sequences of the same kind of stuff, and you deal with the items individually.

    What are these "kinds of stuff" I'm talking about? Types? Sometimes. But types may not tell the whole story.

    Consider the following two data structures:
    Code:
    >>> import time
    >>> time.localtime()
    (2008, 2, 5, 11, 55, 34, 1, 36, 0)
    
    >>> range(10)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    The first one, a tuple, is a sequence in which position has semantic value. The first position is always a year. This tuple functions as a lightweight record or struct.

    The second one, a list, is a sequence where we may care about order, but where the individual values are functionally equivalent.

    It's easy to imagine adding or removing items from the second one without breaking code that handles it or creating something undefined; for the first one, not so much.

    A great example of the complementary use of both types is the Python DB API's fetchmany() method, which returns the result of a query as a list of tuples. The result set as a whole is a list, because rows are functionally equivalent (homogeneous); the individual rows are tuples, because rows are coherent, record-like groupings of (heterogeneous) column data.

    A side note: Many functional languages, including Haskell and OCaml, have a similar distinction that is strictly enforced. Studying one of these languages may help clarify what I'm saying here; it certainly clicked for me when I started playing with Haskell.

    There is considerable overlap in the ways tuples and lists can be used, but the built-in capabilities of the two structures highlight some of the above distinctions. For example, tuples have no 'index' method for identifying the position at which a particular value is found.
    Last edited by Rahul.Patil; 18th November 2012 at 06:05 PM.
    Rahul Patil <http://www.linuxian.com>

  3. The Following User Says Thank You to Rahul.Patil For This Useful Post:

    chimu (25th November 2012)

  4. #3
    Contributors chimu's Avatar
    Join Date
    Mar 2005
    Posts
    92
    Thanks
    28
    Thanked 5 Times in 4 Posts
    Rep Power
    9

    Default

    Thanks a lot.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 28th February 2012, 06:11 PM
  2. on delete cascade differences
    By eawedat in forum Databases servers
    Replies: 0
    Last Post: 24th August 2011, 04:08 AM
  3. [Solved] For loop input backticks vs $(...) differences
    By jaysunn in forum Shell scripting
    Replies: 2
    Last Post: 16th July 2009, 07:02 PM
  4. Differences between Linux and Windows
    By eawedat in forum Linux software
    Replies: 2
    Last Post: 5th July 2009, 03:54 PM
  5. MySQL databse differences
    By unixadmin007 in forum Databases servers
    Replies: 3
    Last Post: 11th June 2009, 03:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41