Back to: Python Programming
List Comprehensions is a concise way to create lists in Python. They are compact and easier to read than traditional loops. They follow the formula that:
For every value in something that’s iterable, meaning you can look through it, check some condition, then do some expression.
The general syntax: list_name = [expression | for value in iterable | if condition]
We have an expression part, an iteration part and a condition part. As each iterable is checked from the list, if the condition is TRUE, then the result is passed to the expression part.
Example: Using a traditional loop, we’ll create a list and double the numbers 1 through 10.
doubles = []
for x in range(1, 11):
doubles.append(x*2)
print(doubles)
Outputs
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
We can use a list comprehension to make this code more compact and easier to read.
We just need to follow the formula.
doubles = [expression | for value in iterable | if condition]
doubles = [x * 2 for x in range(1, 11)]
print(doubles)
Expanding on this with some more examples to triple and square the 10 numbers:
doubles = [x * 2 for x in range(1, 11)]
triples = [y * 3 for y in range(1, 11)]
squares = [z * z for z in range(1, 11)]
print(doubles)
print(triples)
print(squares)
Outputs:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Working with strings – we’ll create a list of fruits then take each string in the list and make it to upper case.
We can either have the result assigned to a new list ie uppercase_fruits = [] or we can reassign the result to overwrite the original list ie fruits = []
fruits = ["apple", "orange", "banana", "coconut"]
uppercase_fruits = [fruit.upper() for fruit in fruits]
print(uppercase_fruits)
OR
fruits = ["apple", "orange", "banana", "coconut"]
fruits = [fruit.upper() for fruit in fruits]
print(fruits)
Both output:
['APPLE', 'ORANGE', 'BANANA', 'COCONUT']
We could simplify the number of lines of code further by placing the iterable list fruits here:
fruits = [fruit.upper() for fruit in ["apple", "orange", "banana", "coconut"]]
print(fruits)
Now we’ll take the first letter of each fruit and put it within a new list called fruit_chars:
fruits = ["apple", "orange", "banana", "coconut"]
fruit_chars = [fruit[0] for fruit in fruits]
print(fruit_chars)
Outputs:
['a', 'o', 'b', 'c']
An example working with conditions – we’ll create a list of numbers, both positive and negative. We’ll create a list comprehension to create a new list where all numbers are positive.
numbers = [1, -2, 3, -4, 5, -6]
positive_nums = [num for num in numbers if num >=0]
print(positive_nums)
In the above example we are mostly focused on the if condition. If the value of num meets the condition >= 0 for each iterated list item, then it is placed in the list (expression part)
Outputs:
[1, 3, 5]
Extending this example:
numbers = [1, -2, 3, -4, 5, -6, -7, 8]
positive_nums = [num for num in numbers if num >=0]
negative_nums = [num for num in numbers if num <0]
even_nums = [num for num in numbers if num % 2 == 0]
odd_nums = [num for num in numbers if num % 2 == 1]
print(positive_nums)
print(negative_nums)
print(even_nums)
print(odd_nums)
Outputs:
[1, 3, 5, 8]
[-2, -4, -6, -7]
[-2, -4, -6, 8]
[1, 3, 5, -7]
A final exercise – create a list of test scores. We want to show only the passing scores that are greater than or equal to 50.
grades = [85,42,79,90,56,61,30]
passing_grades = [grade for grade in grades if grade >= 50]
print(passing_grades)
Outputs:
[85, 79, 90, 56, 61]
List Comprehension – BEST Python feature !!! Fast and Efficient – (14M:50s)