List Comprehension – What Is It And Does It Only Exist In Python?
Tags: programmingconcepts, 100codeexamples, concepts, python
Published: 2021-02-10 10:25:37

---
title: List Comprehension – What Is It And Does It Only Exist In Python?
published: true
date: 2021-02-09 23:07:45 UTC
tags: ProgrammingConcepts,100CodeExamples,Concepts,Python
canonical_url: https://developer-sam.de/2021/02/list-comprehension-what-is-it-and-does-it-only-exist-in-python/
---

Since summer, I have two trainees. One very nice thing about that situation is that I am forced to think about basic concepts in a way I never did before – because now I have to explain and teach them instead of just using them.

And sometimes, I learn new terms – like “list comprehension”.

The term might be known by most Python developers, but I never encountered it so far and when my trainee brought it up I was curious.

Here’s what my current understanding of the concept is:

## What is List Comprehension?

In short, list comprehension is a way in which a programming language supports working with lists and collections in a comfortable way.

In most programming languages, you are able to iterate through lists of objects and do all sorts of manipulations inside that iteration loop, for example creating a new list with just a part of the original content based on some conditions.

If a programming language supports list comprehension, it provides an **easier** way to do that.

Let’s look at an easy Python example where we have a list of dictionaries, holding some famous Star Wars Characters:

```python
starWarsCharacters = (
    {"name":"Darth Vader", "side":"dark"},
    {"name":"Mace Windu", "side":"light"},
    {"name":"Leia Organa", "side":"light"},
    {"name":"Darth Maul", "side":"dark"},
)

newList = []
for i in range(0, len(starWarsCharacters)) :
    if ( starWarsCharacters[i]["side"] == "dark") :
        newList.append(starWarsCharacters[i]["name"])

print(newList)
```

I can use a for-loop to go through all the elements of a list and if they belong to the dark side, put their name into a new list.

Thanks to list comprehension in Python, I can do the same with one single line of code, which is also a lot easier to read:

```python
newList = [character["name"] for character in starWarsCharacters if character["side"] == "dark"]
print(newList)
```

Isn’t that nice? It’s nearly self-explanatory, but let’s go through it step by step.

## Anatomy of List Comprehension

List comprehension usually consists of 3 parts: A source from which to read, a filter (optional) and a mapping that goes into the new list.

In our Python example, the parts are as follows, put into square brackets:

| Part| Example |
| ------ | ------------------------------------- |
| Source | `for character in starWarsCharacters` |
| Filter | `if character["side"] == "dark"` |
| Mapping | `character["name"]` |

The order in which the different parts are arranged in Python is

`<Mapping> for <SourceListElement> in <SourceList> if <Filter>`

## List Comprehension in Other Languages

I’m not sure if the term “list comprehension” is really used in other languages, but when I understood what the term means in Python I recognized that the **concept** exists in a lot of programming languages:

## Python

```python
starWarsCharacters = (
    {"name":"Darth Vader", "side":"dark"},
    {"name":"Mace Windu", "side":"light"},
    {"name":"Leia Organa", "side":"light"},
    {"name":"Darth Maul", "side":"dark"},
)

newList = [character["name"] for character in starWarsCharacters if character["side"] == "dark"]
print(newList)

# Run: py characters_of_the_dark_side.py
```

## JavaScript

```javascript
let starWarsCharacters = [
    {name:"Darth Vader", side:"dark"},
    {name:"Mace Windu", side:"light"},
    {name:"Leia Organa", side:"light"},
    {name:"Darth Maul", side:"dark"},
];

let newList = 
    starWarsCharacters
        .filter(character => character.side == "dark")
        .map(character => character.name);
console.log(newList);

// Run: node characters_of_the_dark_side.js
```

Note that the order in which the different list comprehension parts are arranged are different, but the way it works is pretty similar.

## C-Sharp

To run C# code as script, install the dotnet-scripting tool via

```
dotnet tool install -g dotnet-script
```

or [read about the alternatives](https://itnext.io/hitchhikers-guide-to-the-c-scripting-13e45f753af9).

```csharp
var starWarsCharacters = new[] {
    new {name = "Darth Vader", side = "dark"},
    new {name = "Mace Windu", side = "light"},
    new {name = "Leia Organa", side = "light"},
    new {name = "Darth Maul", side = "dark"}
};

var newList = 
    from character in starWarsCharacters
    where character.side == "dark"
    select character.name;

foreach ( var item in newList ) {
    Console.WriteLine(item);
}

// Run: dotnet script characters_of_the_dark_side.csx
```

The list comprehension feature of C# is called LINQ – and it’s quite awesome.

## SQL

People often forget this, but SQL is really the champion of list comprehension.

To run this example you have to install sqlite, e.g. on Windows via chocolatey:

```
choco install sqlite
```
```sql
with starWarsCharacters as (
    select "Darth Vader" name, "dark" side union all
    select "Mace Windu" name, "light" side union all
    select "Leia Organa" name, "light" side union all
    select "Darth Maul" name, "dark" side
)
select character.name
    from starWarsCharacters character
    where side = "dark";

-- Run: sqlite3 test.db ".read characters_of_the_dark_side.sql"
```

Was this helpful and/or interesting? Let me know ![🙂](https://s.w.org/images/core/emoji/13.0.1/72x72/1f642.png)

The post [List Comprehension – What Is It And Does It Only Exist In Python?](https://developer-sam.de/2021/02/list-comprehension-what-is-it-and-does-it-only-exist-in-python/) appeared first on [Developer Sam](https://developer-sam.de).