List Comprehensions and Nested List Comprehensions in Python (2024)

Understanding List and Nested list comprehensions in Python

List Comprehensions and Nested List Comprehensions in Python (3)

The list is one of the built-in data structures in Python and it is a group of the same or different types of elements. It is a very tedious task to create a list in Python which involves a for loop.

Python is known for its simplicity and easy-to-understand code. If you know the syntax, then Python is extremely easy to use. In this tutorial, we are going to talk about List Comprehensions that make your code more elegant and readable.

Let’s get started.

One of the best features of Python is List Comprehensions it helps you to write code in a single line.

But first, we will see an example in a for loop of creating an exponential list.

List Comprehensions and Nested List Comprehensions in Python (4)

Let’s see what we have done in the above example. First, we created a list of expo variables. Then, we created an empty list of expon variables than a variable of ex iterate over an iterable of the expo. After that, the exponential of ex is created and stored in ex. Now, we append the ex in the empty list of expon. Finally, we print the list.

Now, we will implement the same using map() function which is an alternative approach with the lambda function.

List Comprehensions and Nested List Comprehensions in Python (5)

You have to use the list before the map function otherwise, it will give you the address of the map() not the desired output. Here, you don’t have to define a function explicitly, lambda does it for you.

The map() function takes a lambda function and a list while the lambda function takes one expression and any number of arguments.

The map function will return a new list with the modified values which have been modified by the lambda function. Now, let’s talk about list comprehensions:

List comprehensions provide a short and compact way to create a list. Let’s see the syntax:

new_list = [expression for item in iterable if condition]

  • Square brackets are necessary to get the output as a list.
  • Expression or operation which is executed for each value inside the iterable and is followed by for loop
  • It is an ordinary for loop which is used to iterate over each item in the iterable. Iterable can be a list, set, string, etc.
  • if a condition is optional.

Here, new values are created and stored in another list which is new_list. Values in the old list will remain unchanged.

Let’s see an example:

List Comprehensions and Nested List Comprehensions in Python (6)

Here, each number will be multiplied by itself means we are doing exponentials of each variable, and values are stored in a new list which is called exponentials.

Conditions are used as a filter to get a customized and precise output while creating a new list. Here, the given expression has to meet certain criteria or a condition specified to get a new list.

Let’s take an example with an if condition:

List Comprehensions and Nested List Comprehensions in Python (7)

Here, the values which are evaluated to True with the given condition will be executed and we get an output in a new list. Only the even numbers enter a new list. You can add more than one condition if you want.

Nested List Comprehensions are used to create combinations of lists within a collection. In other words, nested list comprehensions are list comprehensions within another list comprehensions.

Let’s go through some examples to see what we can do with nested list comprehensions:

We take an example to flatten a list of lists:

List Comprehensions and Nested List Comprehensions in Python (8)

Did you understand anything? No. Here, the first for loop is used to access the outer list, and the second for loop is used to access the values in the inner sublist.

Let’s see the same example using a for loop and an append() method.

List Comprehensions and Nested List Comprehensions in Python (9)

You can use filters or conditions with this as well. Let’s see how:

List Comprehensions and Nested List Comprehensions in Python (10)

You can see the condition where the length of the sublist should be greater than 2, if it is not then that sublist will be discarded.

You now know the basics of list comprehension and nested list comprehensions in Python. It offers an elegant and concise syntax for creating new lists based on existing lists or other iterable.

Using too much List comprehension in python will make your code unreadable.

Yes, list comprehension is very useful to write more beautiful and pythonic code. It is always recommended to use list comprehension in Python but maintain the code readability. List comprehension makes the code hard to understand, it is always recommended to avoid list compression in complex cases.

List Comprehensions and Nested List Comprehensions in Python (2024)
Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6195

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.