What's the difference between list and tuples in Python? Which one is recommended ?
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.
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:
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.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 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>
chimu (25th November 2012)
Thanks a lot.
There are currently 1 users browsing this thread. (0 members and 1 guests)