Tuesday, December 24, 2013

List comprehension in Python.


List Comprehension in Pyhton
Python supports various compound data type and one of very important is list.List is similar to array in other languages but also provide more functionality.
A list comprehension provides a compact way creating a list from other list using some operation on the elements of list.It is derived from set notation.This topic is very important as per python based technical interviews are considered. I tried to cover this topic with some basic examples and some real world example.

Suppose we are having a list l and we want to create a new list from l with each element incremented by 1. Here is the simple way to do this.
>>> l=[1,2,3,4]
>>> k=[ele+1 for ele in l]
>>> print k
[2, 3, 4, 5]
>>> k
[2, 3, 4, 5]
>>> 

             
This is called as list comprehension.

Now lets try list comprehension with multidimensional list.


>>> l1=[[1,2,3], [3,4,5] ,[6,7,8]]
>>> col1=[ row[1] for row in l1]
>>> col1
[2, 4, 7]

Here l1 is 2 dimensional list.Each element of l1 is itself a list.So "for row in l1" will select each element of l1 and assign "row" as name to the element.Now each element is list itself. Here we extract second element of each list and append it to col1.

Now suppose we want to construct a new list from l1 so that we want to sum all the element of each sublist of l1 to from single element of new list.Here is the solution.

 
>>> lnew=[sum(row) for row in l1]

>>> lnew

[6, 12, 21]


Lets display this multidimensional list as single dimensional list.

>>> lsdim=[row for row1 in l1 for row in row1 ]

>>> print lsdim

[1, 2, 3, 3, 4, 5, 6, 7, 8] 



 Now suppose we want to display the elements of sublist if and only if they are even number.Here is the solution.
>>> leven=[row for row1 in l1 for row in row1 if row%2 == 0]

>>> print leven

[2, 4, 6, 8]




This are some simple programming tricks of list comprehension in python.

Lets see a simple real world example.

Suppose we want to display the files of root user in current working directory.

Here we have to use the glob module.We import the module and then get the list of all the files in current working directory using glob() function.We create a new list from this list of files using condition that the user id of file must be same as that of root i.e. 0. For getting the user id of file we used stat() function of os module.
>>> lofile=glob.glob('*.*')

>>> lofile

['java.zip', 'parchild.c~', 'inc.cpp', 'inc.cpp~', 'packetcapturing.tar.gz', 'ifstmt.c', 'ifstmt.cpp~', 'weather.cpp~', 'alk.cpp~', 'talk.c~', 'read.sh~', 'Python-3.3.2', 'weather.cpp', 'my.sh', 'inc.c~', 'my.c', 'myc.c', 'a.out', 'ifstmt.cpp', 'mozilla.pdf', 'inc.c', 'myclass.py', 'parchild.c', 'oomp1.cpp~']

>>> lnew=[f for f in lofile if os.stat(f).st_uid ==0 ]

>>> print lnew

['ifstmt.c', 'my.sh', 'my.c', 'myc.c', 'inc.c', 'myclass.py', 'parchild.c']