List comprehension in python

Introduction

We all probably know about the versatility of the python language. Writing code in python is very easy and refined. List comprehension is one of the exclusive features of this language which helps to write code in a compact form or fewer lines.

Before we begin, I would suggest you for learning the basics of Python lists if you are not familiar with it.

The Syntax

See the below syntax to know how the list comprehension is done.

new_list = [expression for item in iterable]

or

new_list = [expression for item in iterable conditional statement]

Example 1

To learn list comprehension, solving various problems is very important. Here and in the upcoming section, we will go through several problems and solve each of those by using list comprehension. Let's start with a simple one.

Problem: Store all the even numbers from a given list to a new list.

Without list comprehension

First, we will solve the problem using the common way.


num = [1,2,3,4,5,6]
new_list = []
for i in num:
if i%2==0:
new_list.append(i)
print(new_list)

Using List Comprehension

Now see, how the above problem can be solved using the list comprehension method and what is the difference here.


num = [1,2,3,4,5,6]
new_list = [i for i in num if i%2==0]
print(new_list)

Output

[2, 4, 6]

Example 2

Let's solve two more similar problems.

Problem 1: Store squares of all the numbers between 1 to 10 in a list using list comprehension.


square = [i**2 for i in range(1,11)]
print(square)

Output

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Problem 2: Store the first letter of every word from a given list to another list using the list comprehension method.


names = ['PySeek', 'Python', 'List', 'Comprehension']
new_list = [name[0] for name in names]
print(new_list)

Output

['P', 'P', 'L', 'C']

List comprehension with two conditions

So far we have solved various problems but those were pretty straightforward. Let's make it a little bit conditional. Here, we will add multiple conditions to the list comprehension statement.

Problem: Iterate through a list of integer numbers; if the number is even, then square of it and store in another list, else store the number as a negative form. Solve this problem using only the list comprehension method.


Numbers = [1,2,3,4,5,7,8,9,10,11,12,13,14,15,16]
newList = [i**2 if (i%2==0) else -i for i in Numbers]
print(newList)

Output

[-1, 4, -3, 16, -5, -7, 64, -9, 100, -11, 144, -13, 196, -15, 256]

Nested list comprehension in python

A nested loop is a loop inside another loop and the parent loop can have any number of child loops. To show you an example I have taken a simple problem where one for loop is within another for loop and the result will be stored in a python list using the list comprehension method.


nested_comp = [[i for i in range(1,5)] for j in range(4)]
print(nested_comp)

Output

[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post