Comprehending List Comprehension

List Comprehension has easily been one of the best features that I’ve encountered while learning python. It is more readable and looks more cleaner in my opinion, in this blog I will be explaining the way I understand List Comprehension.

First lets start with the syntax of a list comprehension.

 name_of_list = [what you want in this list]

Now from left to right, we would start with assigning this list a name very simple and straight forward. Next we get to the list itself wrapped in brackets. In these brackets we are defining what we want this list to be, we are giving instruction on how to navigate another list or how to manipulate another list to become the list we really want.

Lets go ahead and have an example of a list of strings:

fruits = ["apple", "banana", "watermelon", "papaya", "peaches"]

Now lets say we want our list comprehension to return the strings in fruits list it it has 6 characters or less. lets label our list first.

updated_fruits = []

Okay now lets think about how we would want to instruct our list comprehension to iterate over the list of fruits. After all wed need to pass over each fruit individually to check if it has 6 characters or less to be allowed into our updated_fruits list. So we know that we need to look at each individual fruit in our fruits so lets try this.

[fruit for fruit in fruits]

So lets dissect what we are saying here. We are going to start from left to right, the first fruit we are stating is representing the fruit that we want in our list. We are saying this is the fruit that we are looking for, but of course we have to rummage through every fruit in the list(basket if you like analogies). So thats what leads us to “for fruit in fruits” this is saying we want to look at every fruit in this list. We want every fruit in fruits and we want to check if its gonna match up to our requirements of the fruit that we want in our list. So to properly give meaning our second “fruit” is representing each individual fruit in the list and then we say “in fruits” meaning in our list labeled fruits.

Just to really hammer the point home

[fruit for fruit in fruits] == i only want the fruit of fruit inside of fruits….

Now granted we are not finished so this just seems like an incomplete sentence at this point.

So now that we know that our list comprehension is going to iterate through our list lets give it our conditional(requirement).

[fruit for fruit in fruits if len(fruit) <= 6]

now we added “if len(fruit) <= 6” so as we’ve seen before starting our conditional with an “if”. Then followed by “len(fruit)” and “len()” just gives us the length of what we pass in it, so in this instance we are passing in the string of each individual fruit and it will return the number of characters in the string that was passed. Next we have “<= 6” which we know is less than or equal to 6.

Piecing it together:

[if len(fruit) <= 6] == if the number of characters in fruit is less than or equal to 6

So if we can look at what these two pieces of the list comprehension separately, we notice that the first portion is basically a for loop and the second portion is a conditional if statement. Which is what makes list comprehensions so cool, they allow you to wrap all this in one single line of code that makes it easier to read and write in my opinion.

So now lets put all of it together and see what we are saying:

updated_fruits = [fruit for fruit in fruits if len(fruit) <= 6]

so here we are saying updated_fruits is going to be a list of fruits []. In which we want each fruit in our list to be a fruit in the fruits list but only if each fruit has 6 characters or less. so in return we should be getting:

print(updated_fruits)
#['apple', 'banana', 'papaya']

That is only one example of a list comprehension, I do hope that this inspires you as much as it has me in the possibilities of what you can do when working with list comprehensions.

Happy Coding!!