# List Comprehension Python

Posted on

List comprehensions are a more meaningful and efficient way to generate lists in python. A list comprehension provides a concise way to create a list using a for loop. Mostly problems that are tackled with list comprehensions can also be implemented using a normal for loop, but with a list comprehension, code quantity is less and efficient. List comprehensions are an elegant way to build a list without having to use different for loops to append values one by one. These examples might help. The simplest form of a list comprehension is. Previous Chapter: Lambda Operator, Filter, Reduce and Map
Next Chapter: Generators

## List Comprehension

### Introduction

We learned in the previous chapter 'Lambda Operator, Filter, Reduce and Map' that Guido van Rossum prefers list comprehensions to constructs using map, filter, reduce and lambda. In this chapter we will cover the essentials about list comprehensions. List comprehensions were added with Python 2.0. Essentially, it is Python's way of implementing a well-known notation for sets as used by mathematicians.
In mathematics the square numbers of the natural numbers are, for example, created by { x2 x ∈ ℕ } or the set of complex integers { (x,y) x ∈ ℤ ∧ y ∈ ℤ }.
List comprehension is an elegant way to define and createlist in Python. These lists have often the qualities of sets, but are not in all casessets.
List comprehension is a complete substitute for the lambda function as well as the functionsmap(), filter() and reduce(). For most people the syntax of list comprehension is easier to be grasped.

### Examples

In the chapter on lambda and map() we had designed a map() function to convert Celsius valuesinto Fahrenheit and vice versa. It looks like this with list comprehension:The following list comprehension creates the Pythagorean triples:Cross product of two sets:

## List Comprehension Python For Loop

Generator comprehensions were introduced with Python 2.6. They are simply a generator expression with a parenthesis - round brackets - around it.Otherwise, the syntax and the way of working is like list comprehension, but a generator comprehensionreturns a generator instead of a list.

### A more Demanding Example

Calculation of the prime numbers between 1 and 100 using the sieve of Eratosthenes:We want to bring the previous example into more general form, so that we can calculate the list of primenumbers up to an arbitrary number n:If we have a look at the content of no_primes, we can see that we have a problem. There are lots of double entries contained in this list:The solution to this intolerable problem comes with the set comprehension, which we will cover in the next section.

### Set Comprehension

A set comprehension is similar to a list comprehension, but returns a set and not a list.Syntactically, we use curly brackets instead of square brackets to create a set.Set comprehension is the right functionality to solve our problem from the previous subsection. We are able to create the set of non primes without doublets:

### Recursive Function to Calculate the Primes

The following Python script uses a recursive function to calculate the prime numbers. It incorporates thefact that it is enough to examine the multiples of the prime numbers up to the square root of n: Previous Chapter: Lambda Operator, Filter, Reduce and Map
Next Chapter: Generators

List comprehensions are a more meaningful and efficient way to generate lists in python. A list comprehension provides a concise way to create a list using a for loop.

Mostly problems that are tackled with list comprehensions can also be implemented using a normal for loop, but with a list comprehension, code quantity is less and efficient.

## Generating a list with for loop

This for loop iterates over the characters in 'apples' and them to a list, newlist. But this is too much code for such a simple task, we can achieve this easily and concisely with list comprehensions.

## Generating a List with list comprehensions

You can see that a list comprehension creates a list more easily and concisely than using only a for loop.

Example 2

## Python List Comprehension Multiple Variable

Suppose you have a list of fruits, and you want to create a new list containing only the fruits which have an 'e' in it:

with a for loop only,

This can also be achieved in List comprehensions with the use of filters.

## Filtering list comprehensions

Filters in list comprehensions are just conditions(if else elif) in a list comprehension.

The above process of creating a new list with fruits having the letter 'e' can be simplified as Toad sql free.

## List Comprehension Python Dictionary

If you want a list of fruits that have more than 5 letters:

If you want a new list containing the fruits in uppercase:

More Examples:

To create a new list containing the squares of numbers in another list:

## List Comprehension Python Else

To generate a list containing the squares of even numbers in another list:

## Functions and List comprehensions

With list comprehensions, you can generate a list from the values returned from a function. Let's take a look at a function that takes in a person's name and returns it in uppercase with a greeting:

output