Understanding Comprehensions in Python

DS - VRP
5 min readNov 14, 2021

--

I wanted to learn List comprehension so I decided to learn and write an article about this.

Life will be easier with comprehension. Comprehensions are a short form of iteration, generator and function, for the function we use lambda (an anonymous) function.

There are many built-in functions in Python, we can perform and practice every python function. Comprehension consists of all iterators and generators. Comprehension can include all conditional statements and built-in functions.

  1. List Comprehension: The list is a built-in data structure in Python, it is mutable and can include all data types like integers, float, boolean and string.
For loop

In the above example, we created a list consisting of all data types, it includes data structures like list and tuple. Then we have created a new empty list and with the help of for loop assigning values to the empty list. Now we try with the help of a list comprehension.

List comprehension

first, we make a list [ ], as we want the result in form of a list, then we take n as we want to print each item from the list, then iteration follows.

we can find the code is short, it will be tricky to use and understand, but it can save our time to write lines of code.

List comprehension with the condition

for loop with condition

we can find that we are assigning only even numbers to the myList. in the second line of code, we can find the if statement with the modulus operator, helps us to get the even number from the list nums. Below we can find the compression of the above code.

List comprehension with condition

2. Dictionary Comprehension: like a list, we can short the code for the dictionary too. Dictionary consists of Key and values pairs.

First, we need to create two lists then assign an empty dictionary to iterate the lists with the help of for loop, we can find a zip method in the first line of code. zip method takes iterable and returns an iterable object. Below is an example of dictionary comprehension.

Dictionary Comprehension

3. Set Comprehension: A set is an unordered and mutable collection of unique elements.

An Example of set

First, we are creating a list with duplicate values. we are calling set with variable my_set. while iterating we are using add method instead of the append.

Set Comprehension

The syntax for the set compression is like list compression with curly brackets.

4. Generator Comprehension: A generator facilitates the creation of a custom iterator. It uses the yield keyword.

Syntax for Generator

As shown, we need to define a function, with iterable. we are using the yield keyword instead of return or print.

Generator Comprehension

we are creating a my_gen generator, the syntax of the generator is the same as list compression with parenthesis. we are using for loop for calling the generator.

Map, Filter and Reduce and lambda function

map, filter and reduce are inbuilt functions and lambda is an anonymous function.

Map: It helps us to assign a function to the list.

We can see that in the result we are trying to take out a list of squared numbers by using Map.

Filter: it helps us to filter the list based on the condition given.

we can see that the evens are the list of even numbers as we are filtering the nums with help of the is_even function.

Reduce: reduce function help us to provide a function to ‘iterables’ and returns a single value. It belongs to functools library we need to import it.

Reduce function

first, we created a function sum with two arguments and then use the reduce function to add all elements of the list.

Lambda function: lambda function is an anonymous function. whenever we find that a function cannot be called again then we can use a lambda function.

Lambda function with Map
Lambda function with filter
Reduce function

As we can see how lambda function reduced the lines of code.

feel free to give your valuable suggestions.

Please connect on LinkedIn

Thank you

--

--

DS - VRP
DS - VRP

Written by DS - VRP

An aspiring data scientist on a journey of continuous learning and discovery—turning curiosity into insights and challenges into opportunities to innovate

No responses yet