List Comprehension

Sarmita Majumdar
3 min readNov 17, 2020

A comprehensive way to describe a sequence or a list using Python

Photo by Yue Iris on Unsplash

A huge time we spend while creating a List in a program. The tedious job includes:

  1. Creating empty List
  2. Introduce Loop
  3. Adding elements one by one…

Python cares about us and provides a tool to simplify our burden with a simple, single line of code. It’s List Comprehension.

The list is a collection of Data, surrounded by a bracket, and the elements in the list are separated by commas. List Comprehension is also surrounded by a bracket, but, instead of data inside, we enter the expression followed by a loop.

Syntax: [expression ||Loop in||Collection of Data] → will evaluate each element present in the Data list.

Let’s try with an example

  1. To generate a list of the square for 100 integers.

2. Another little bit complex example to find the Remainder while dividing with 5.

3. Sometimes we have to solve the problem where we have to make decisions with the IF clause. Say, our TASK is to find Movie names from a list of movies where the movie name starts with “N”.

4. We will now make the clause bit complicated with Tuple. Say, the above example contains a combination of movie names with the corresponding year of release. Our test is to display those movie names whose release dates are within 2018.

Here, we can understand that for loop contains variables inside braces because movies are representing the year in individual tuples.

Let’s talk about Scalar multiplication with Vector.

Let’s assume a vector v=[1,2,3] , for example, if we go for simple multiplication by 4, it will give us : 4* v = v+ v + v + v .

What happens here the 4 times v is adding up vectors like this, and in Python, if we add two lists it concatenates rather than add them component-wise.

So, using list comprehension we can solve this to receive the required output.

Cartesian Product

Say we have two sets, A and B then the Cartesian Product will be a set of pairs i.e. (a, b), where ‘a’, the first component is in A and ‘b’, the second component is in B. This was introduced by the creative mathematician Rene Descartes and the mathematical formula is :

Executing the following we will get all the 16 possible pairs. This technique helps us to generate Cartesian products for three or more sets.

Now, in mathematics, it’s a challenge while we are going to retrieve reminders by dividing with Prime numbers! Here is an example that will generate an interesting pattern.

The problem of finding which number in the list is a complex puzzle.

Conclusion

List comprehensions offer a short & sweet process to create lists. The perception behind is more fast and efficient than for loop. We can simplify complicated problems. List comprehensions can be used when a list of output is required as a map. This returns only a map object and does not return any list. And, as we know Map is a faster way to call an already defined function.

--

--

Sarmita Majumdar

Data Scientist. Is in love with Python that allows the user to work on multiple domains ranging from Data Science, Machine Learning, Deep Learning, AI, and more