Monday, March 16, 2020

List Comprehension In Python Language


List Comprehension

In Python Language, the list is the most versatile data type. A list consists of different items or data separated by commas and enclosed between square brackets ([]). The list in Python is similar to the arrays in C,
C++ and Java programming. The main difference between the arrays and the list is that array is the collection of the same type of data types wherein the list we can have different types of data. Now suppose we want to create the empty list we can use the following syntax:
l=[]
print(type(l))
We can also create an empty list by using the built-in list type objects. For example, we can create the list by using the following code:
l=list()
The Python Language also supports the computed lists called list comprehensions. We can also be called the list comprehension is a concise way of creating the list. This is similar to Math's set builder notation. Suppose we have to represent the set of squares of natural numbers from 1 to 10 then we can use the following methods.
square=[]
for i in range(1,11):
    square.append(i**2)
print("The square of numbers from 1 to 10 is", square)
The output of the above code result in:
The square of numbers from 1 to 10 is [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
The same can be written as follows:
square=[(x*x) for x in range(1,11)]
print(square)
 The output of the above code is
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
The syntax for creating the list comprehension is as follows:
list=[ expression for variable in sequence ]
Where the expression is evaluated once, for every item in the sequence.
So in every list comprehension, we have the following parts:
  1. the expression is the member itself which is used to call a valid expression that can return some values. In the above example (x*x) is the expression.
  2. member is the value whose lies between the sequence provided in the list.
  3. iterable is a list, sequence or any other item that can return the single value at a time.
So with the help of List comprehension, we can create very easily the lists consists of many items. List comprehension helps programmers or coders to create the lists concisely. This is mainly beneficial to make new lists where each element is obtained by applying some operations to each member of another sequence or iterable. We should note that an iterable is the object that can be used repeatedly in subsequent loop statements. let us take another example of list comprehensions. As we know the list comprehension consists of a square bracket containing an expression followed by a for clause, then zero or more for or if clauses. Suppose we have the following list numbers:
numbers=[-1, 2, 34, -9, -10, 7, 8, 9, 121, 0]
And now if we want to get all positive numbers in the list then we can use the following code:
numbers=[-1,2,34,-9,-10,7,8,9,121,0]
p=[x for x in numbers if x>=0]
print(p)
The output of the above code will be as follows:
[2, 34, 7, 8, 9, 121, 0]
So from the above code, we can deduce that here x is bound with every value in the list numbers that are -1, 2, 34, -9, -10, 7, 8, 9, 121 and 0. But the if clause will filter only value where the condition x>=0 is true i.e. 2, 34, 7, 8, 9, 121 and 0. 
Now let us take another example if we want to print all the vowels in the given sentence we can use list comprehension in the following ways:
sentence="This is the sentence which is used to check the power of the list comprehension"
vo=[x for x in sentence if x in 'aeiou']
print(vo)
The output of this code will be as follow:
['i', 'i', 'e', 'e', 'e', 'e', 'i', 'i', 'u', 'e', 'o', 'e', 'e', 'o', 'e', 'o', 'e', 'i', 'o', 'e', 'e', 'i', 'o']
And suppose we have to do vice-versa the following code will work:
sentence="This is the sentence which is used to check the power of the list comprehension"
co=[x for x in sentence if x not in 'aeiou']
print(co)
The output will be:
['T', 'h', 's', ' ', 's', ' ', 't', 'h', ' ', 's', 'n', 't', 'n', 'c', ' ', 'w', 'h', 'c', 'h', ' ', 's', ' ', 's', 'd', ' ', 't', ' ', 'c', 'h', 'c', 'k', ' ', 't', 'h', ' ', 'p', 'w', 'r', ' ', 'f', ' ', 't', 'h', ' ', 'l', 's', 't', ' ', 'c', 'm', 'p', 'r', 'h', 'n', 's', 'n']
We can also use the list comprehensions to combines the elements of two list. Consider the following code:
print([(x,y) for x in [10,20,30] for y in [30, 40, 10] if x!=y])
The output of the code will be 
[(10, 30), (10, 40), (20, 30), (20, 40), (20, 10), (30, 40), (30, 10)]
In the code, two values, one from each list is used to create a new list only if the two values are not same.
Now with all the above examples, we come to the conclusion that Python language had provided a strong tool for a list comprehension. This can be easily used to deduce the long codes into the smaller and easily understandable codes. As lists are mutable so they can be easily appended and list comprehension is one of the methods to create the long list where the data or item is generated with the help of the following two ways:
  • expression
  • filters
The filters can be easily combined to get the desired list as we have done in extracting the vowels or constants.
We not only create list comprehensions but we can also create the dictionary comprehensions the only difference is that we require the key-value pair in it. So to utilize the complete power of list comprehension lies in the hand of programmer or coder in the way he or she can utilize. So enjoy the coding and try to reduce the effort of typing using list comprehensions.

Tutors Group

Home Tuition & Home Tutors Jobs Portal



No comments:

Post a Comment