Friday, April 19, 2019

Python | List comprehension

Hello Pythonists,
In my new blog I would like to share a new concept of python that I had learnt recently.
Its about the Python List comprehension and why we would be using it?
List comprehension is a more simple way to define and create list in python, we can create lists in one line. The syntax of list comprehension is easier to grasp as well.

List comprehension generally contains below things:

  •  An expression, which will be the output list
  •  An Input sequence
  •  A variable representing member of input sequence and
  •  An optional predicate part, where we can define our conditional statements


There can be multiple ways of performing a task in any programming language, similarly with python list comprehensions as well.
In my article we will straightaway not use list comprehension but will study the concept in a relational way, comparing it with loops and then conclude the study, as I found this way lot easier to understand.

Syntax of List Comprehension:
[expression for item in list]

Understanding syntax in details:
e.g [i**2 for i in range(100) if i%2==0 and i%5==0]
i**2 --> is expression
for i in range(100) --> input sequence and i is the variable
if i%2==0 and i%5==0 --> literals and its an optional part
In below examples we will develop a better understanding of the concept.


1. Suppose I want to create a list of sequential numeric items, staring from 1 to 10. Let's check what all are the possible ways to create it.
Method 1. Using for loop
Code:
my_list=[]
for i in range(10):
    my_list.append(i)
print(my_list)

Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Method 2. Using List comprehension
Code:
my_list=[i for i in range(10)]
print(my_list)

Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]



#Note: Just a single line code using comprehensions

2. Suppose I want to multiply items of a given list with some number.
Method 1. Using for loop
Code:
new_list=[]
my_list=[5,4,8,7,2,9]
for i in my_list:
    new_list.append(i*i)
print(new_list)

Output: [25, 16, 64, 49, 4, 81]

Method 2. Using list comprehension
Code:
my_list=[5,4,8,7,2,9]
new_list=[i*i for i in my_list]
print(new_list)

Output: [25, 16, 64, 49, 4, 81]


3. List comprehension with conditionals, extract even numbers and add it to another list
Mehtod 1. Using for loop
Code:
my_list=[5,4,8,7,2,9]
new_list=[]
for i in my_list:
    if i%2==0:
        new_list.append(i)
print(new_list)

Output: [4, 8, 2]

Method 2. Using list comprehension
code:
my_list=[5,4,8,7,2,9]
new_list=[i for i in my_list if i%2==0]
print(new_list)

Output:
[4, 8, 2]



4. with multiple conditional statements

Method 1. Using for loops
Code:
new_list=[]
for i in range(100):
    if i%2==0 and i%5==0:
        new_list.append(i)
print(new_list)

Output: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

Method 2: Using list comprehension
Code:
new_list=[i for i in range(100) if i%2==0 and i%5==0]
print(new_list)

Output: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]



5. To create a new list of odd square number.
Method 1. Using for loops
Code:
odd_sqr=[]
for i in range(1,10):
    if i%2 != 0:
        odd_sqr.append(i**2)
print(odd_sqr)

Output: [1, 9, 25, 49, 81]

Method 2. Using list comprehension
Code:
odd_sqr=[i**2 for i in range(10) if i%2!=0]
print(odd_sqr)

Output: [1, 9, 25, 49, 81]


6. Nested List comprehension:
create a list of lists using nested loops and list comprehensions:
Method 1. Using for loops
Code:
l=[]
for i in range(5):
    row=[]
    for j in range(5):
        row.append(j)
    l.append(row)
print(l)

Output:  [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]

Method 2. Using List comprehension
Code:
l=[[j for j in range(5)] for i in range(5)] print(l)

Output:  [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]

In nested list comprehensions, inner loop passes value to the outer loop, in above example value of i is passed to j in every iteration.




So folks, with demonstration from above examples I hope I am able to make my point on what exactly is list comprehension and what is the reason we use them. As we can see in above examples how it can reduce the code into a single line and readability is better.
Thanks for reading my blog and I would be thankful If you guys can share your reviews/feedback so that I can improve my content.
I would like to thank to a reader for suggesting me to include Nested List Comprehensions.

Thanks
Vivek Chaudhary

No comments:

Post a Comment