---
title: "Discussion on \"8 explained examples of list comprehension in Python\" | Hashnode"
description: "Discussion on \"8 explained examples of list comprehension in Python\". What is list comprehension\nList comprehension is a concise way used instead of for-loop when you want to create a new list based on another iterable (list, tuple, dictionary, set, string, generator, ...).\nYou get to do this:\n[do_this(element) for ele..."
url: "https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [8 explained examples of list comprehension in Python](https://girlthatlovestocode.com/8-explained-examples-of-list-comprehension-in-python?utm_source=hashnode&utm_medium=feed)

What is list comprehension List comprehension is a concise way used instead of for-loop when you want to create a new list based on another iterable (list, tuple, dictionary, set, string, generator, ...). You get to do this: \[do\_this(element) for ele...

Apr 13, 2021 · 9 min read · girlthatlovestocode.com  
Updated Apr 21, 2021  
Read the article

-   [#python](https://hashnode.com/tag/python)
-   [#python3](https://hashnode.com/tag/python3)
-   [#python-beginner](https://hashnode.com/tag/python-beginner)

## Written by

[GirlThatLovesToCode](https://hashnode.com/@GirlThatLovesToCode)

@GirlThatLovesToCode · Slovenia

  
[View profile](https://hashnode.com/@GirlThatLovesToCode)

Self-taught developer that wants to support everyone that are learning to code, especially women.

I'm passionate about learning new things -- both for myself and for teaching others. I'm a fan of Python and Django and want to know everything there is about those two. When I'm not writing code or a blog, I'm probably trying something new, reading, or spending time outside with my family.

## Responses 9

Markdown supported · ⌘↵ to post

[Brian Ketelboeter](https://hashnode.com/@briancode1975)

[Apr 16, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/6079a0f5767ccc06dae34e83)

it is a small thing, but you sometimes use 'array' (non-python) when you mean 'list' (python). Again, very small.

[Apr 21, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/6079a0f5767ccc06dae34e83#reply-607fc563c99e200d08f81cc4)

Thank you for pointing that out, I fixed it.

[Brian Ketelboeter](https://hashnode.com/@briancode1975)

[Apr 16, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/60799ec3767ccc06dae34e6b)

A nice skill to discuss. Excellent choice of article content and clear explanation

[GirlThatLovesToCode](https://hashnode.com/@GirlThatLovesToCode)

[Apr 21, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/60799ec3767ccc06dae34e6b#reply-607fc4cbc99e200d08f81cae)

Thank you, I'm glad you liked it.

[Kah](https://hashnode.com/@kahdev)

[Apr 16, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/60797e16c7bf370315009611)

Nice list. I once used comprehension to divide a list into smaller groups. For example:

```
>>> source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]                                    
>>> [source[i:min(i + group_size, len(source))] for i in range(0, len(source), group_size)]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13]]
```

[GirlThatLovesToCode](https://hashnode.com/@GirlThatLovesToCode)

[Apr 21, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/60797e16c7bf370315009611#reply-607fc4bbeb2bd30d2d22c85f)

Great idea, thanks for sharing (:

[Siddharth Chandra](https://hashnode.com/@chandraji)

[Apr 15, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/6077db6ba3407a32d7042b03)

Totally agree with you on that list comprehensions work faster than traditional loops. Generally, if we write more pythonic code, it will always be more time-efficient I think.

[GirlThatLovesToCode](https://hashnode.com/@GirlThatLovesToCode)

[Apr 15, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/6077db6ba3407a32d7042b03#reply-6077e82fa3407a32d7042c00)

It's definitely more pythonic. But I did some research and it should be faster on simpler cases, but not on more complicated ones.

[Siddharth Chandra](https://hashnode.com/@chandraji)

[Apr 15, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/6077db6ba3407a32d7042b03#reply-6077e9074155da32fc83bb7b)

Yeah, I feel like as the complexity increases, python code execution tends to be slower irrespective of its pythonic nature 😅

[GirlThatLovesToCode](https://hashnode.com/@GirlThatLovesToCode)

[Apr 15, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/6077db6ba3407a32d7042b03#reply-6077ea9fa3407a32d7042c4b)

This is an interesting idea 😅

[Mohammed Sameeruddin](https://hashnode.com/@msameeruddin)

[Apr 14, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/607705784155da32fc83aaad)

Nice write up. 😀

Don't you think, the heading "Convert dollars to euros" should actually be "Convert euros to dollars".

Moreover the function name is also `convert_to_dol()`.

[GirlThatLovesToCode](https://hashnode.com/@GirlThatLovesToCode)

[Apr 15, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/607705784155da32fc83aaad#reply-6077ea67a3407a32d7042c2f)

Thank you for noticing, I fixed it.

[Amal Shaji](https://hashnode.com/@amal)

[Apr 13, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/60755497308cd51e14780215)

Super cool post. List comprehensions are very useful. I've got some alternate solutions to make life harder.

### Simple Filtering

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1618301078690/UolWzvlfp.png)

### Filtering and applying some operation

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1618301186409/tW9cUh4T7.png)

### Combining all elements

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1618302012588/tjKJtPnrh.png)

### Common elements

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1618301583320/OUdurbb59.png)

### Using it with a function

Use `map(Callable, Iterable)`

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1618301811996/qFqGMP5Uk.png)

[GirlThatLovesToCode](https://hashnode.com/@GirlThatLovesToCode)

[Apr 15, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/60755497308cd51e14780215#reply-6077ea86a3407a32d7042c47)

Thanks for making life harder :P

[Tosin Ayodele](https://hashnode.com/@devtosxn)

[Apr 16, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/60755497308cd51e14780215#reply-6079d49d5af33606b2563d08)

Exactly my point when I went through the work [GirlThatLovesToCode](https://hashnode.com/@GirlThatLovesToCode)

[Vince](https://hashnode.com/@vinceknight)

[Apr 13, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/6075539a308cd51e14780204)

This is a great resource, thank you for writing it. I'll be adding it to my list of extra reading for my class.

[GirlThatLovesToCode](https://hashnode.com/@GirlThatLovesToCode)

[Apr 15, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/6075539a308cd51e14780204#reply-6077e852a3407a32d7042c0d)

This is a comment every author wants to see (:

[Jan Giacomelli](https://hashnode.com/@jangia)

[Apr 13, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/6075456f85f3a21e3926c8fd)

Great post

[GirlThatLovesToCode](https://hashnode.com/@GirlThatLovesToCode)

[Apr 13, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/6075456f85f3a21e3926c8fd#reply-60754ba7308cd51e1478015e)

Thanks (:

[GirlThatLovesToCode](https://hashnode.com/@GirlThatLovesToCode)

[Apr 13, 2021](https://hashnode.com/posts/8-explained-examples-of-list-comprehension-in-python/60754383308cd51e1478008c/comment/6075453585f3a21e3926c8d9)

The solutions for the practice ideas:

`[(model, color, size) for model in models for color in colors for size in sizes]`

`princesses_from_frozen = [frozen_char for frozen_char in frozen for princess in princesses for i_like in princesses_i_like if frozen_char == princess and frozen_char == i_like]`

`[F"{name} {surname} - {age}" for name, surname, age in zip(names, surnames, ages) if int(age) > 20]`
