Hi Readers
In this blog, we'll review how to use list comprehension in Python. They are handy for simplifying and shortening code and allow us to skip specific steps.
What is List Comprehension?
Creating lists is a relatively simple process, some are used throughout your code as parameters while others are more valuable. Many times, to populate our list object, we use for
loops which can have more complicated syntax. List comprehension allows us to instantiate our list object and loop to fill its values in a single line.
For Example:
Below is the standard syntax to create a list of numbers with their cubed values.
cubed_list = list()
for num in range(1, 11):
cubed_list.append(n ** 3)
print(cubed_list)
# [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
As you can see, we are defining an empty list called cubed_list
. We then use a for
loop with a range from 1 to 10 to append their cubed values to the list. Printing cubed_list
at the end will show its populated values.
However, using list comprehension, we can complete this process much more simply. It enables us to accomplish the same feat in one line. Below is the syntax of the list comprehension interpretation of the same demo we did above.
cubed_list = [(num ** 3) for num in range(1,11)]
print(cubed_list)
# [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
As you can see, both methods of functionality accomplish the same list of cubed values although the syntax for list comprehension is a little different.
It still uses the same "chunks" as the previous method, but refactors it to exist on one line. The base code for list comprehension is as follows.
new_list = [operation(i) for i in old_list]
We are declaring a variable new_list and setting it equal to a value enclosed in square brackets. The first chunk is an optional operation(i)
that handles what values populate new_list
. In our earlier example's case, we used (num ** 3)
. The second chunk is for i in old_list
. In this example, i
is what we're using as our parameter to represent each item in old_list
.
Conditionals
With list comprehension, we can also add conditions to determine whether or not the for loop takes place for a particular item in each list. The code for conditionals in list comprehension is added to our previous example below.
cubed_list = [(num ** 3) for num in range(1,11)]
print(cubed_list)
# [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
evens_list = [num for num in cubed_list if num % 2 = 0]
odds_list = [num for num in cubed_list if num % 2 = 1]
print(evens_list)
# [8, 64, 216, 512, 1000]
print(odds_list)
# [1, 27, 125, 343, 729]
Here we are adding an if
statement at the end, separating the cubed values into two lists, odds and evens.
Wrapping up
List comprehension is a very useful tool for writing our code in a way that is simple, efficient, and presentable. That is one of the primary benefits of populating a list object this way. Reading from left to right, the language means exactly what its functionality is.
evens_list = [num for num in cubed_list if num % 2 = 0]
"evens_list equals a list, populate that list with num
for num in
cubed_list
if that num is even."
The way we described the functionality is exactly how the syntax is designed.