One of the tenants of python is "Readability Counts". If you 'import this' you'll notice Readability as being the seventh line in the Zen of Python. If you're coming from a C flavored language though (JavaScript, Java, C#) then some of the syntax may come off as rather alien at initial glance. One such feature of Python that is immensely powerful and very different is the list comprehension.

Man holding a python sticky note

The Form of List Comprehensions

The form is as follows:  [what we want returned + what we're iterating through and any filters]

 

Basic Usage

Let's say we have a dictionary of users with the key being that user's name and the value being their favorite cheese. We want to create an array of the users who like cheddar:

cheese.fromage_users = {
   
'bob': 'cheddar',
   
'lilly': 'american',
   
'tony': 'swiss',
   
'jenny': 'cheddar',
   
'ella': 'muenster',
}

likes_cheddar = []
for user, cheese in fromage_users.items():
   
if cheese == 'cheddar':
       likes_cheddar.
append(user)

Ah the classic for / in loop. A tried and true approach, but breaks the rule of Readability Counting. We can make this into one line:

likes_cheddar = [name for (name, cheese) in fromage_users.items() if cheese == 'cheddar']

 

Opening a File with List Comprehensions

The traditional way to make a list from a file would be to open up the whole file, split the lines and loop over to append to our final list.

f = open('./cheeses.txt')
cheeses = f.read().split('\n')
cheeses_list = []
for cheese in cheeses:
    cheeses_list.append(cheese)

Here we've made this into a concise line of code. Now armed with this we can get fancy. Let's say we want to open a file:

cheeses_list= [line.split('\n') for line in open('cheeses.txt')]

You've now got an array of arrays for each line break in a text file. Helpful, but let's go one deeper while keeping it practical.

 

Converting Header/Content into Useful Object

What if you received a CSV and need to make a useful array of objects out of it? For example:

header = ['user','age','favorite_cheese']
content = [[
'bob', 42, 'mozzerrela'],
[
'cindy', 28, 'provolone'],
[
'samantha', 99, 'blue'],
[
'ralph', 5, 'feta']]

An initial approach might be to have an empty array, loop through the content, and push a formatted user/age/favorite_cheese object for each user in the content. Again, tried and true, but we can simplify this with the 'zip' keyword.

users = [dict(zip(header, user)) for user in content]

Which in turn outputs what we're looking for:

[{'user': 'bob', 'age': 42, 'favorite_cheese': 'mozzerrela'},
{
'user': 'cindy', 'age': 28, 'favorite_cheese': 'provolone'},
{
'user': 'samantha', 'age': 99, 'favorite_cheese': 'blue'},
{
'user': 'ralph', 'age': 5, 'favorite_cheese': 'feta'}]

List comprehensions are one of the built in patterns to Python that makes the language both elegant and sparse. Using these to replace more monotonous patterns when generating lists will make your code easier to read (for those in the know) and abide by some of the core tenets of the Zen of Python.

Ben Sadick
Author Bio

Ben Sadick is a Software Engineer at Jahnel Group, Inc., a custom software development firm based in Schenectady, NY. Jahnel Group is a consulting firm that specializes in helping companies leverage technology to improve their business operations. We provide end-to-end strategic consulting and deployment services, helping companies optimize their operations and reduce costs through the use of technology. Jahnel Group is an Advanced Tier AWS Services Partner, with expertise in AWS Lambdas, Amazon API Gateway, Amazon DynamoDB, and other AWS services.

Unlock Your Future!

Send us a message