The Proficient Pythonista: List Comprehensions
Tags: python, codequality, datascience
Published: 2020-02-29 18:06:16

---
title: The Proficient Pythonista: List Comprehensions
published: true
description: 
tags: #python #codequality #datascience
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/4h1n1ybo1f1mn8xp99zs.png
---



*For* loops are a thing! But if you could sometimes use a construct that is way more concise and efficient won’t you go for it? Hell Yeah!!!

List comprehensions give us a succinct way to create lists based on existing lists.

To create a new list of numbers from an existing list using a *for loop* construct:
{% gist https://gist.github.com/joyadauche/a1439680926c4066a1bd4975306a4a74 file=1.py %}

What if I told you what we have in the loop above can be done in just a single line code! - This is the power of list comprehensions. Take a look below in *2.py*:
{% gist https://gist.github.com/joyadauche/a1439680926c4066a1bd4975306a4a74
file=2.py %}

The structure for the code written in *2.py* above is **[ output expression *for* iterator variable *in* iterable ]**: 
 - *number + 3* is the output expression - the result returned
 - *number* is the iterator variable
 - *numbers* is the iterable

List comprehension can be written over any iterable like a range object and not just lists as below:
{% gist https://gist.github.com/joyadauche/a1439680926c4066a1bd4975306a4a74 file=3.py %}



We can also have conditionals in list comprehensions which helps filter what we get returned - it controls what items from an existing list are returned. This conditional logic can be on the **iterator variable** or the **output expression**.

In *4.py* below is an example of a conditional logic used on the iterator variable, where it returns only items that are not equivalent to the string *cisco*:
{% gist https://gist.github.com/joyadauche/a1439680926c4066a1bd4975306a4a74
file=4.py %}

In *4.py* above, the structure for the code written is **[output expression *for* iterator variable *in* iterable *if* predicate expression ]** -
 - *If flash != ‘cisco’*  - the conditional logic which is an **if predicate expression**

Let’s take it further by having nested if predicate expressions within a list comprehension:
{% gist https://gist.github.com/joyadauche/a1439680926c4066a1bd4975306a4a74
file=5.py %}

In *5.py* above, it checks to see if the number *num* is divisible by 2 and then 4, and prints it out if it satisfies both conditions.

Moving on to *conditionals on the output expression*, let us see an example below:
{% gist https://gist.github.com/joyadauche/a1439680926c4066a1bd4975306a4a74
file=6.py %}

The structure for the code written in *6.py* above is  **[conditional on output expression  *for* iterator variable *in* iterable]** - 
 - *hero if len(hero)>=8 else ''* - the conditional logic - outputs *hero*
   if the length of the string hero is less than 8, else it
   outputs an empty string.



Like list comprehensions, we can also have a **dictionary comprehension** or even a **set comprehension**. This is getting quite interesting right?

With set comprehensions, the output returned unlike a list comprehension contains no duplicates. Let’s see an example in *7.py* below:
{% gist https://gist.github.com/joyadauche/a1439680926c4066a1bd4975306a4a74
file=7.py %}

In *7.py*, no duplicates are returned. Also, note that sets like dictionaries but unlike lists are inherently unordered - the order of items is not important - this is why *o* comes first in the set even if *e* is the first character in *cool_quote*.

Talking about dictionaries, let’s see an example of how we can use a dictionary comprehension to create a dictionary below:
{% gist https://gist.github.com/joyadauche/a1439680926c4066a1bd4975306a4a74
file=8.py %}

In *8.py* above, we have a *key and value* pair separated by a colon (:) - the keys are the items of the list while the values are the length of the items in the list.



Nested loops are also something we do sometimes. See an example below:
{% gist https://gist.github.com/joyadauche/a1439680926c4066a1bd4975306a4a74
file=9.py %}

The code above is multiplying the items in the first list by the items in the second list. To write this in a list comprehension, it is written as below:
{% gist https://gist.github.com/joyadauche/a1439680926c4066a1bd4975306a4a74
file=10.py %}

In *10.py* above, the outer list comprehension **[*... for i in range(5, 8)*]** creates 3 rows, while the inner list comprehension **[*i*j for j in range(1,3)*]** fills these rows with values i.e i*j.



Very good use of list comprehension is to flatten a list consisting of multiple lists as seen below:
{% gist https://gist.github.com/joyadauche/a1439680926c4066a1bd4975306a4a74
file=11.py %}



List comprehensions are superb! But I think you can still use for loops in certain places where code readability is key because it is important to also write what your team can read easily. I hope you are excited about your journey to becoming a Proficient Pythonista!!! :wink:

Stay tuned to this series for my next article on **GENERATORS**! Have an amazing and fulfilled week ahead!