Making Sense of List Comprehensions in Python

Making Sense of List Comprehensions in Python

A Quick Look at Lists

Lists were one of the first things I learned about when I started exploring Python, and it wasn’t long before I realized they are a commonly used feature of the programming language. A list in the Python language is an ordered and mutable data structure that contains a collection of items stored between a set of brackets []. Lists can contain duplicates, and the individual items within a list can be called by using a corresponding index value, starting at [0,…,n]. Items within a list can be removed, changed or added, making lists an adaptable way to work with data. There are a ton of resources out there to help you learn more about python lists, but here is an example:

#example list
color_list = ["green", "red", "blue", "yellow"]

Starting with List Comprehensions

One of the “pythonic” ways of working with lists is called list comprehensions. A list comprehension is a syntactic construct that allows you to create a list based on existing lists. The general syntax is as follows:

#new_list = [(output value)(for loop)(logical conditions)]#example list comprehension
green_list = [color for color in color_list if color == 'green']

List comprehensions can dramatically reduce the amount of code you need to complete an operation. For example, this is the same logic using a for loop instead of list comprehension:

green_list2 = []
for color in color_list:
    if color == 'green':
        green_list2.append(color)

My Eureka! Moment with List Comprehensions

Before I started learning Python, I was familiar with SQL and used it daily in my job. SQL (Structured Query Language) is commonly used to manage data in relational databases like MySQL and Microsoft SQL Server. When I was a python beginner, lists seemed fairly straight-forward and easy to apply in a lot of ways, but list comprehensions were difficult for me to understand. I was intimidated by the syntax, so I avoided using them. One day, while working through a python exercise, I was writing a list comprehension and suddenly realized the syntax resembles an SQL SELECT statement. It was a Eureka! moment for me and gave me confidence to get creative.

#SQL select statement example
#SELECT color FROM color_list WHERE color IN ('green', 'red', 'blue')#list comprehension selecting the same data as the SQL statement
rgb = [color for color in color_list if color in('green', 'red', 'blue')]
print(rgb)

It is easy to see how the list comprehension resembles an SQL select statement.


More List Comprehensions

After my eureka moment, studying list comprehensions became a lot easier. Although a powerful tool, there are a few things to remember. List Comprehensions can be rewritten as for loops, but not every for loop can be written as a list comprehension! Also, writing out a for loop might make sense insofar as readability and maintainability.
Here are a few more examples of using list comprehensions:


Two ways to map values using list comprehensions

#create numeric indicators for colors
color_indicator = [0 if color == 'green'else 1 if color == 'red' else 2 if color == 'blue' else 3 for color in color_list]
print(color_list)
print(color_indicator)

color_mapping = {'green': 0, 'red': 1, 'blue':2, 'yellow':3}
color_indicator2 = [color_mapping[color] if color in color_mapping else 'na' for color in color_list]
print(color_list)
print(color_indicator2)

Finding Items of Length n

Lets say we want to find all colors in our list that are longer than 5 characters

long_color_words = [color for color in color_list if len(color) > 5]
long_color_words

It is simple to replicate between logic too. We can find all colors with a length between 4 and 6:

color_length5 = [color for color in color_list if len(color) > 4 and len(color) < 6]
color_length5

Nesting Loops

You can nest loops to perform operations and return a matrix. Here we create a matrix of color attributes:

color_list1 = ['green', 'red', 'blue', 'yellow']
color_list2 = ['dark', 'bright', 'tinted', 'glowing']color_matrix = [[color2 + ' ' + color1 for color1 in color_list1] for color2 in color_list2]
color_matrix


Final Remarks

Hopefully this helps make sense of Python list comprehensions and gives you a simplified way to think about them if they seemed intimidating at first. They are an elegant and powerful way to work with list data, so understanding them is an important step in advancing in Python. Thinking about list comprehensions as if they were SQL Select statements helped me wrap my head around the concept and helped me explore the functionality with more confidence.

Source: towardsdatascience