My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping


Saturday, November 24, 2012

Python - Tuple Vs List - Difference/ Comparison

Tuples and Lists have always attracted the learners of Python. And, to the most of employers, the difference between them still maintains favoritism. Searching over the Internet gives you the distinction between them, but to many the language used all over seems to not help much. And worse part is that, not all the differences have ever been published at one place. And the worst of all, sometimes technical slangs have been used, with no direct meaning or examples to make it clear.

Let me start with a very brief introduction to what these Datatypes are about. Following description of List and Tuple is an excerpt from my "How To Tame Python" guidebook.

List is one of the most helpful and versatile data type available in Python. A list contains several items separated by commas. The items are enclosed within square brackets ([]). The list comes with built in functions which can be used to add, delete, edit contents in a list. Any subset of a list returned is also a list. Also note that like strings here also the Index starts from 0 and goes to the Length of the list minus 1.
A typical list can be as simple as :
list=[2,4,8,16,32], or
Or it can be a mix of datatypes for eg.

Tuple again is a data type that is similar to the list. However, elements of a tuple are enclosed within parentheses. [Remember elements in a list were enclosed in square paranthesis]. It is important to mention that a Tuple is a read-only list. Any subset of a tuple returned is also a tuple. Also note that like strings, and list, here also the Index starts from 0 and goes to the Length of the tuple minus 1.
Eg. tup1=(12,'leo',23.45)

After completing my "How To Tame Python" guidebook, addressing this issue was the most immediate task. I am thankful to the memories of my sweetheart, while she was away, that kept me awake till night and motivate me in finding out the real differences. I actually programmed and validated each statement I said below. There are differences in Tuples and a List. I have discussed them in following classes of distinctions. So, here comes the difference between Tuples and List in Python.

Class A:  Usage difference due to Runtime difference 
Tuples are Immutable. 'tuple' object does not support item assignment
Because they are immutable, a Tuple can be used as a Key in a Dictionary.
There may be a slight performance improvement in Tuple.
For eg.
Allocation in a book can be a combination of a page number and line number, eg (x,y)
Similarly, your gps coordinates can be (x,y)
It is a tuple because these things are immutable. As soon as either x or y changes, the meaning itself changes.
And now, some short notes can be added to these locations using a dictionary.

Class B:  Difference in what they mean due to Semantic difference
Tuples are hetrogenous data structure, while lists are homogenous. There is a semantic difference too. Tuples have structure, while lists have order. Tuples (generally) are sequences of different kinds of stuff, and you deal with the tuple as a coherent unit. But, Lists (generally) are sequences of the same kind of stuff, and you deal with the items individually.
Eg 1.
Consider the following two data structures:
>>> time.localtime()
(2012, 3, 7, 11, 45, 38, 2, 33, 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.
Eg. 2
Consider example of the simultaneous use of both types. If you look the Python Database API fetchmany() method, you will see that it returns the result of any query as a "List of Tuples". The result set as a whole is a list, because rows are functionally equivalent (homogeneous). There is only a order, and no importance which row is what. But the individual rows are tuples, because rows are coherent, record-like groupings of (heterogeneous) column data. We know that first element means what and second elements mean what, and so on.

Class 3:  Usage as Enum or Stuct because of namedtuple() method

This difference becomes more visible as soon as you read about namedtuple in Python
Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code. They can be used wherever regular tuples are used, and they add the ability to access fields by name instead of position index. Named tuples are especially useful for assigning field names to result tuples.
collections.namedtuple(typename, field_names, verbose=False, rename=False)
It Returns a new tuple subclass named typename. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable.
EmployeeDetail = namedtuple('EmployeeDetail', 'id, name, age, job, department, paygrade,address')

I have realized these differences after years of coding and using Python. Many of the differences, were always in sub-conscious mind and never came out. It was only when I was completing my "How To Tame Python" guidebook, that the idea of documenting the differences came to my mind. And, after this has been done, I was more than glad to find out that this is the best article over this subject. I have been informed by my friends and readers across the globe that they have found this article very helpful in their projects, understanding the Python, and their Job interviews.

You can also send me your feedbacks on this blog, or to my personal email id:
--Mohd Anwar Jamal Faiz

No comments:

Post a Comment