Understanding list, set and dict comprehensions

Just few days ago, you were having a good time with your friends and counting down to 2017. Few days have passed and you are left with a typical cold snowy day in January. You are busy writing code for a high profile project at work. Suddenly, a situation arises where you need to create a new list from an existing one. You code it like you have always been coding:

>>> old = ['adam', 'mike', 'olga']
>>> for name in old:
        new.append(name+ ' last')
>>> new
['adam last', 'mike last', 'olga last']

But then you realize that one of your 5 new year’s resolutions is to start using list comprehensions! You have heard about them but were always a little intimidated by them. You were also not really sure of their point.

This post will help you with your new year’s resolution. However, it won’t help you with the other one about going to gym 4 times a week.

List Comprehensions
Because the syntax for generating lists is very common, python has a concise way of doing this via list comprehensions.

>>> old
['adam', 'mike', 'olga']
>>> [x+" last" for x in old]
['adam last', 'mike last', 'olga last']

As you can see above, I have used a list comprehension to generate our new list in just one line. List comprehensions don’t require any initialization of the new list or the need to use .append() in a loop.

Code for list comprehensions is contained in brackets []. The actual code begins with the content of the new list:

x+" last"

and then the first line of a usual for loop:

for x in old

 
Conditional list comprehensions
What we just covered is known as unconditional list comprehensions. But what about conditional list comprehensions where you only want to generate a new entry for the list if a condition is true?
Lets go back to our old example and only add ” last” to each entry if the original entry contains character a.

This amazing drug really took the world by storm about cheap cialis cheap cialis when it was introduced in 1998 to an eager public. The first big decision after deciding to host a baby shower discount cialis is to choose what type baby shower they want to host. Erectile Dysfunction condition itself is often related to an individual’s age, gender and genes which are not even prescribed by an expert ED spelevitra properien https://www.unica-web.com/archive/2019/johanna-maria-paulson-jury-member-2019.htmlt. It is viagra doctor free so annoying to hear the incessant shouting in the streets, restaurants and any other places without any regard for other people. Here is how you would write this without list comprehension:

>>> new=[]
>>> for name in old:
        if 'a' in name:
                new.append(name+ ' last')
>>> new
['adam last', 'olga last']

With list comprehension:

>>> [x+' last' for x in old if 'a' in x]
['adam last', 'olga last']

As you can see, the syntax is mostly the same except that you add the if statement in the end.

Once you understand the concept and syntax of list comprehensions, it can be applied to other data structures as well such as sets and dicts. The idea is still the same: to create new sets and dicts from an existing data structure.

For set comprehensions, just enclose the code in braces {} instead of brackets []. For example:

>>> {x+' last' for x in old if 'a' in x}
set(['olga last', 'adam last'])

Similarly, for dict comprehensions:

>>> old_dict = {'first_name': 'Adam', 'last_name': 'Cooper'}
>>> old_dict
{'first_name': 'Adam', 'last_name': 'Cooper'}
>>> {key:value+ ' last' for key,value in old_dict.items()}
{'first_name': 'Adam last', 'last_name': 'Cooper last'}

 
Conclusion
List, set and dict comprehensions are just instances of syntactic sugar. They might seem a bit overwhelming at first but after you have had some practice using them in your code, they will come naturally to you.

Bonus: Using parenthesis () to enclose your code will create a generator.

>>> (x+' last' for x in old if 'a' in x)
<generator object <genexpr> at 0x1007cdf00>

Leave a comment

Your email address will not be published. Required fields are marked *