List comprehensions are a feature of Python that can make your code more readable and less time-consuming. They are a tricky feature to get your head around, but once you do you'll find they can really come in handy.
In this blog post, we will explore the basics of list comprehensions and how to use them in Python.
Creating a list using a normal for-loop
fruit = "apples" newlist =  for char in fruit: newlist.append(char) print(newlist) #['a', 'p', 'p', 'l', 'e', 's']
This for-loop iterates over the characters in "apples" and creates a list, "newlist". But this is too much code for such a simple task, we can achieve this easily and concisely with list comprehensions.
Creating a list using a list comprehension
fruit = "apples" newlist = [char for char in fruit] print(newlist) #['a', 'p', 'p', 'l', 'e', 's']
You can see that a list comprehension creates a list more easily and concisely than using only a for-loop.
Filtering a list
Suppose you have a list of fruits, and you want to create a new list containing only the fruits which have an 'e' in it:
Using a for loop only:
fruits = ["apple", "banana","date", "orange", "cherry", "kiwi", "mango"] newlist =  for f in fruits: if "e" in f: newlist.append(f) print(newlist) # ['apple', 'date', 'orange', 'cherry']
This can also be achieved in List comprehensions with the use of filters
Using list comprehensions:
You can easily create a new filtered list with list comprehensions using if/else statements. The above process of creating a new list with fruits having the letter 'e' can be simplified as
fruits = ["apple", "banana","date", "orange", "cherry", "kiwi", "mango"] newlist = [f for f in fruits if "e" in f] print(newlist) # ['apple', 'date', 'orange', 'cherry']
If you want a list of fruits that have more than 5 letters:
newlist = [f for f in fruits if len(f) > 5] print(newlist) # ['banana', 'orange', 'cherry']
If you want a new list containing the fruits in uppercase:
newlist = [f.upper() for f in fruits] print(newlist) # ['APPLE', 'BANANA', 'DATE', 'ORANGE', 'CHERRY', 'KIWI', 'MANGO']
To create a new list containing the squares of numbers in another list:
nums = [2, 3, 1, 5, 6, 4, 12, 3] squares = [i*i for i in nums] print(squares) # [4, 9, 1, 25, 36, 16, 144, 9]
To generate a list containing the squares of even numbers in another list:
nums = [2, 3, 1, 5, 6, 4, 12, 3] squares = [i*i for i in nums if i%2==0] print(squares) # [4, 36, 16, 144]
Using list comprehensions with functions
With list comprehensions, you can generate a list from the values returned from a function. Let's take a look at a function that takes in a person's name and returns it in uppercase with a greeting:
def greet(name): greeting = "Hello " + name.upper() return greeting people = ["John", "Doe", "Prince", "Abdul", "Isaac"] greet_everyone = [greet(p) for p in people] print(greet_everyone)
['Hello JOHN', 'Hello DOE', 'Hello PRINCE', 'Hello ABDUL', 'Hello ISAAC']
List comprehensions can make your code efficient if you use them correctly.
Read More: 8 Excellent Python Courses on Udemy (2021)
Leave a reply