Python is always known for easy syntax which make it easy to write and read codes like we writing or reading a letter in plain English language. It has many easy method for various task that quit long or difficult in many other languages, this is only reason why python is popular among various programmers world wide.
One of language distinct and powerful features is list comprehension to work on list in which we can write one line code for python list with condition and statements to get new list out of it. It make code short and easy to use although some developers used some complex comprehensions. Let’s try to understand how and when we can use List comprehensions.

Syntax

Wo write some list comprehensions, we could use some simple method like below.

[ expression for value in collection ]

This could be more complex if we also need to add some conditions into into it.

[ value for value in list if condition ]

Examples

Whenever we need it work in any list, we usually write some for loop to iterate it like loop get time to apply conditions on every element of list. This possible could take 3-4 lines of code,but though list comprehensions we could write it only in one line of code .Let’s take an example.

>>> country_list=["America","India","China","Germany","Afghanistan","Colombia","Greece","Italy"]
>>> for country in country_list:
...     if country.startswith("A"):
...             print(country)
... 
America
Afghanistan

In above code we tried to identify country name from list which country name has first letter A.

But what if we need them in list to work on further, here we need create empty list before for loop and append it like below.

>>> country_a=[]
>>> for country in country_list:
...     if country.startswith("A"):
...             country_a.append(country)
... 
>>> print(country_a)
['America', 'Afghanistan']

What if we can write this piece of code in just one line though python List comprehension like below.

>>> [country for country in country_list if country.startswith("A")]
['America', 'Afghanistan']

save in variable

We can also assign it to list in same line of code.

>>> country_a=[country for country in country_list if country.startswith("A")]
>>> print(country_a)
['America', 'Afghanistan']

We can also write some complex List comprehensions like below , which help to measure various CPU Cores temperatures. Here we tried to call method inside list comprehensions

>>> import psutil
>>> [i[0:2] for i in psutil.sensors_temperatures()['coretemp']]
[('Package id 0', 30.0), ('Core 0', 28.0), ('Core 1', 29.0), ('Core 2', 29.0), ('Core 3', 27.0), ('Core 4', 30.0), ('Core 5', 28.0), ('Core 6', 30.0), ('Core 7', 27.0)]

>>> [i[0:2] for i in psutil.sensors_temperatures()['coretemp'] if i[1]>28]
[('Package id 0', 30.0), ('Core 1', 29.0), ('Core 2', 29.0), ('Core 4', 30.0), ('Core 6', 29.0)]

We can also write if condition after this, like if current temperature is more then 28 Celsius, so we can evaluate some conditions as well inside list comprehensions

Convert to dictionary

We can make it more useful through saving it in dictionary, this make it useful to use further use.

>>> {i[0]:i[1] for i in psutil.sensors_temperatures()['coretemp'][1:]}
{'Core 0': 28.0, 'Core 1': 29.0, 'Core 2': 30.0, 'Core 3': 26.0, 'Core 4': 30.0, 'Core 5': 27.0, 'Core 6': 30.0, 'Core 7': 27.0}

With above examples , we now know List comprehensions are great way to write python code which make it more readable, easy to understand and debug for long run.