Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Python Programming. No Comments on Comprehensions in Python Programming
0
(0)

In this article we will look at comprehensions in Python programming language. We will look at what is a comprehension in Python and different types of comprehensions.

 

Introduction

Python comprehensions are constructs that create sequences from existing sequences in a clear and concise manner. Comprehensions are of three types:

 

  • list comprehensions
  • set comprehensions
  • dict comprehensions

 

List comprehensions were introduced in Python 2.0; while set and dict comprehensions have been introduced in Python 2.7.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


List Comprehensions

List comprehension is the most popular Python comprehension. It allows us to create a new list of elements that satisfy a condition from an iterable. An iterable is any Python construct that can be looped over like lists, strings, tuples, sets. In list comprehensions we use square brackets.

 

General syntax of a list comprehension is as follows:

[expression for item1 in iterable1 if cond1
	       for item2 in iterable2 if cond2
	       ...
	       for itemN in iterableN if condN]

 

Following is an example of list comprehension for creating a list of squares of numbers in range 1-10:

s=[x*x for x in range(1,11)]

 

In the previous example, we can also use if condition to generate only squares of even numbers:

s=[x*x for x in range(1,11) if x%2==0]

 

Result of above comprehension will be:

[4, 16, 36, 64, 100]

 

List comprehensions can be useful to perform matrix computations. Consider following matrix:

m=[[1,1,1],[2,2,2],[3,3,3]]

 

We can print the diagonal elements as follows:

[m[i][i] for i in range(0,3)] which prints [1 2 3]

 

We can calculate sum of elements in each row in a matrix as follows:

[sum(row) for row in m] which prints [3 6 9]

 

Let our matrix be:

m=[[1,2,3],[4,5,6],[7,8,9]]

 

We can perform transpose of above matrix by writing:

[[row[i] for row in m] for i in range(0,3)] which gives:

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

 

Set Comprehensions

Set comprehensions were added to python in version 2.7. In set comprehensions, we use the braces rather than square brackets. For example, to create the set of the squares of all numbers between 0 and 10 the following set comprehension can be used:

>>> x = {i**2 for i in range(10)}
>>> x
set([0, 1, 4, 81, 64, 9, 16, 49, 25, 36])

 

Dict Comprehensions

Just like set comprehensions, dict comprehensions were added to python in version 2.7.

 

Below we create a mapping of a number to its square using dict comprehension:

>>> x = {i:i**2 for i in range(10)}
>>> x
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

Your email address will not be published. Required fields are marked *