Comprehensions in Python:
The comprehension consists of a single expression followed by at least one for
clause and zero or more for
or if
clauses.
There are three comprehensions in Python.

List Comprehensions:
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable or to create a subsequence of those elements that satisfy a certain condition. — python docs
Syntax:
[expression for item in iterable if conditional]
The expression can be any arbitary expression, complex expressions, tuple, nested functions, or another list comprehension.
This is equivalent to
for item in iterable:
if conditional:
expression
Return Type:
List
Using List Comprehension:
A list comprehension consists of brackets[]
containing an expression followed by a for
clause, then zero or more for
or if
clauses. The result will be a new list resulting from evaluating the expression in the context of the for
and if
clauses that follow it.

1. List comprehension vs for loop.
By using List Comprehension, it is more concise and readable comparing to for loop.
Finding square of numbers using List Comprehension vs for loop:
2. List comprehension vs filter.
filter:
Returns an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator
Finding Even Numbers Using List Comprehension vs filter():
3.List Comprehension vs map.
map:
Return an iterator that applies a function to every item of iterable, yielding the results.
Finding square of numbers using List Comprehension vs map():
4. Nested loops in List Comprehension.
List comprehension can contain one or more for clause.
Example 1: Flatten a list using List Comprehension with two 'for' clause:
5.Multiple if condition in List Comprehension.
List comprehension can contain zero or more if clause.
Example: Finding numbers that are divisible by 2 and 3.
6. The expression can be tuple in List Comprehension.
We can mention expression as a tuple in a list comprehension. It should be written within parentheses. Otherwise, it will raise Error. The result will be a list of tuples.
Example 1: Creating a list of tuples using List Comprehension with two 'for' clause:
If the expression is a tuple and if not enclosed within parentheses, it will raise SyntaxError.
Example 2:Using zip() function in List Comprehension:
7. List comprehension can be used to call a method on each element.
Example 1: Calling strip() method on each element in the list. It is used to strip the white spaces.
Example 2: Calling the upper() method on each element in the list.
8. List comprehension can contain complex expressions and nested functions.
Example 1:In the below example, in expression we are using the strip method and int function.
Example 2: In the below example, in expression, we are using abs() and str() function.
9. Nested List Comprehension.
The expression in a list comprehension can include another list comprehension also.
Example: First List comprehension given as expression will return a list of even numbers from 0 to 10. Nested list comprehension will return that expression (list of even numbers from 0 to 10) three times(range(3)).
Set Comprehensions:
Like List comprehension, Python supports Set Comprehension also.
Set comprehension is written within curly braces{}
.Returns new set based on existing iterables.
Return Type: Set
Syntax:
{expression for item in iterable if conditional}
How to find even numbers using set Comprehension:
How to find the square of numbers using Set Comprehension.
If condition in Set comprehension:
Below example, we are calculating the square of even numbers. The expression can be a tuple, written within parentheses. We are using if condition to filter the even numbers. Sets are unordered. So elements are returned in any order.
Dictionary Comprehension:
Like List and Set comprehension, Python supports Dictionary Comprehension.
A dict comprehension, in contrast, to list and set comprehensions, needs two expressions separated with a colon followed by the usual "for" and "if" clauses. When the comprehension is run, the resulting key and value elements are inserted in the new dictionary in the order they are produced.
Dictionary comprehension is written within curly braces{}
.In Expression key and value are separated by :
Syntax
{key:value for (key,value) in iterable if conditional}
Return Type: dict
How to find the square of numbers using Dictionary Comprehension.
How to iterate through two sets using dictionary comprehension:
If Condition in Dictionary Comprehension:
Zero or more if clause can be used in dictionary comprehension. In the below example, we are calculating the square of even numbers.
Finding the number of occurrences using dictionary comprehension:
In the below example, we are calculating the number of occurrences of each character in a string. We can call the method count()
on each element in the string. count()
will return the number of occurrences of a substring in the string.
We are calculating the number of occurrences of each word in the list by calling count()
on each word in the list.
Generator Expression:
A generator expression yields a new generator object. Its syntax is the same as for comprehensions, except that it is enclosed in parentheses instead of brackets or curly braces.
Generator Expression is written within
()
.It yields a generator. The generator is a function that returns an object (iterator) which we can iterate over (one value at a time). The performance improvement from the use of generators is the result of the lazy (on-demand) generation of values, which translates to lower memory usage. — python docs
Return Type: Generator object
Conclusion:
- Return Type List Comprehension-List Set Comprehension-Set Dictionary Comprehension — dict Generator Expression-Generator Object
- Tuple comprehension is not supported. A generator expression is written within
()
.Its syntax is the same as List Comprehension. It returns a generator object. - List comprehension is written within
[]
Set comprehension is written within{}
Dictionary comprehension is written within{}
- A dict comprehension, in contrast, to list and set comprehensions, needs two expressions separated with a colon.
- The expression can also be tuple in List comprehension and Set comprehension. If an expression is tuple, it should be written within
().
Resources(Python Documentation):
List Comprehensions:
https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
Displays for lists, sets, and dictionaries:
Watch this space for more articles on Python and DataScience. If you like to read more of my tutorials, follow me on Medium, LinkedIn, Twitter.
Thanks for reading!