what is List Comprehension in Python programming language? | applied electronics engineering


what is List Comprehension in Python programming language?

By Applied Electronics - Sunday, May 22, 2016 No Comments
List comprehension is a technique to manipulate elements in a list to form another list or perform action according which element is currently being processed. A list comprehension consist of functional part and conditional part that acts on the elements of a given list by looping through the elements using loop and membership operator.

[New List] = [Functional part + (Loop + Membership)[List] + Conditional part]

list comprehension

Example of functional part are ord(x), float(x), x**2 etc. The loop structure is usually a For Loop and the membership operator is the in operator. The conditional part is usually if or if-else statement.

For loop containing if statement achieve the same thing as list comprehension but list comprehension is more elegant and readable. The core philosophy of python programming language is to make programming language more easy to understand to human. Another philosophy behind list comprehension is to make it resemble the way of writing mathematical expression.

Using Iterative For Loop in List Comprehension

First let's us look into example of list comprehension that uses for loop.

Most simple example of List Comprehension

Consider you have a list of numbers called num:

num = [1, 2, 3 ,4, 5]

Suppose you want to square each number in the num list and store it in another list called newnum. This using list comprehension method is as follows.

newnum = [x**2 for x in num]

the output is,

>>> newnum
[1, 4, 9, 16, 25]

If you were to do this using for loop you would have to do in the following way:

newnum = []
for x in num:
...     newnum.append(x**2)
>>> newnum
[1, 4, 9, 16, 25]

As you can see from this example using list comprehension method reduces code writing and also is more elegant to read, like the mathematician way of writing.

Yet there is another alternative way of achieving the same. This one uses the map( ) method together with list() method.

One way of using map is to define the function first:

def square(x):
...     return x**2

Then use the list(map( ) ) as follows:

newnum = list(map(square,num)
>>> newnum
[1, 4, 9, 16, 25] 

The second method is to use the function lambda directly in the map:

>>> newnum = list(map(lambda x: x**2, num))
>>> newnum
[1, 4, 9, 16, 25]

Using conditional statement in List comprehension

We can also use if the statement inside the list comprehension. Consider the same num list and suppose we want to get the hexadecimal representation of each element if the number in num is greater than 3.

newnum = [hex(x) for x in num if x > 3]
>>> newnum
['0x4', '0x5'] 

The if condition applied the integer to hexadecimal conversion only when the number in num was greater than 3 which is 4 and 5.

In other words the list comprehension has the expression syntax:

new_list = [<function/ statement> for value in old_list <condition> ]

The optional Function or statement such as hex(x) or x**2 is first declared. Then using the for and in keywords of the for loop and membership operator, each element in the old list are processed one by one. Optional condition statement for processing appears at the last in the expression.

Both the function and conditional statement in list comprehension are optional. But the rule is one of them must.

For example, the function is optional. To do nothing to the element using function we just leave the value of the list element as it is. Code example below illustrates this.

newnum = [x for x in num if x%2 == 1]
>>> newnum
[1, 3, 5] 

We wrote simple x in the functional part of the list comprehension instead of writing a function. But we still imposed a condition in the conditional part of the list comprehension thereby modifying the elements.

No Comment to " what is List Comprehension in Python programming language? "