Python one-liner to transform and filter lists
In this article I want to introduce list comprehensions. With a list comprehension you can make a new list by looping over an existing list or other iterable like tuple, string, dictionary or set.

List transformations
Suppose we have a Python list: numbers = [10,120,50,70,240,20,190].
If we needed to make a new list in which each item would be tripled: numbers_tripled = [30,360,150,210,720,60,570], we could use a for loop.
We would start with making a new empty list numbers_tripled = []. And then loop over the orignal numbers list to append each tripled number to the new list.
numbers = [10,120,50,70,240,20,190]
numbers_tripled = []
for number in numbers:
numbers_tripled.append(3 * number)Using a list comprehension to transform a list
By using a list comprehension we can do the same as in the above example but we will only need one line:
numbers = [10,120,50,70,240,20,190]
numbers_tripled = [3*n for n in numbers]The formula to make a list comprehension is:
- Use the square brackets at the start and end:
[] - Define over what iterable you want to loop. In our case we want to loop over each number
nin the listnumbers:[for n in numbers] - Use the iterable’s items to make the transformation. In our case the list’s items are called
n. We will triple eachnby using3*n:[3*n for n in numbers]
Step by step each number in the numbers list is taken, a transformation (in this case a calculation) is performed on it and the result gets added to the new list. It works just like our for loop example, but in one line.
Filtering lists
If we have the same list of numbers numbers = [10,120,50,70,240,20,190] we could use a for loop to filter the list. Filtering a list means taking only certain items of it based on a condition.
Let’s make a list of numbers that are bigger than 100 out of the numbers list.
big_numbers = []
for number in numbers:
if number>100:
big_numbers.append(number)Using a list comprehension to filter a list
A list comprehension can also be used to filter a list. Here is 1 line of code to do the same as the 4 lines of code above.
big_numbers = [n for n in numbers if n>100]The formula of a list comprehension that filters is:
- Use the square brackets at the start and end:
[] - Define over what iterable you want to loop. In our case we want to loop over each number
nin the listnumbers:[for n in numbers] - Specify the condition that needs to be met for a variable to be put in our new list:
[for n in numbers if n>100] - Specify what you want to put in the new list, in our case just
n. But we could also put a transformation onn, for example triple it with3*n:[n for n in numbers if n>100]
Step-by-step each item of the numbers list is taken, we check if a condition is met and if so we put it in our new list.
Other types of comprehensions
To illustrate the possibilities of using comprehensions here are some examples:
From string to list
Result: ['a', 'b', 'c', 'd', 'e']
text = 'abcde'
letters = [l for l in text]
print(letters)From list to string including a transformation
Result: Hello
numbers = [72,101,108,108,111]
text = ''.join(chr(n) for n in numbers)
print(text)From tuple of tuples to dictionary including a filter
Result: {'USA': 8133, 'Germany': 3359, 'Italy': 2452, 'France': 2436}
gold_reserves = (('USA',8133),('Germany',3359),('Italy',2452),
('France',2436),('Russia',2299))
gold_reserves2 = {ctry:gold for ctry,gold in gold_reserves if gold>2300}
print(gold_reserves2)Thank you for reading!
You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:
You might also like:
