In Python, List Comprehension provides a concise and efficient way to create a new list. The new lists can be created with conditions and filters, which will be applied to the existing list.

Syntax of Python List Comprehension
The following is the syntax of the Python List Comprehension:
new_list = [expression for item in iterable]
Parameters:
- expression: The expression determines what gets added to the new list
- item: Item taken from iterable
- iterable: sequential data types, such as list, tuple, string, etc.
Return Value:
- This method returns a new list after applying the expression and conditions.
Python List Comprehension Example
Let us see a simple example showing how list comprehension works.
Example
numlist= [4,32,6,76,12,37,52]
square = [sol ** 2 for sol in numlist]
print(square)
Output:
[16, 1024, 36, 5776, 144, 1369, 2704]
Explanation:
In the above example, we created a list of numbers named sqrt_num. Then, using list comprehension, we created a new list named square that gives the square of every number in sqrt_num.
Difference between For Loop and List Comprehension
The primary difference between a for loop and list comprehension is that for loops often consist of multiple lines to iterate through elements and build a new list. On the other hand, list comprehension takes only one line of code to create a new list. Hence making it clean, readable and much more efficient.
Let’s see an example to understand this clearly:
1) Using For Loop
In this example, we are using a for loop to find the square of the number in the list.
Example
numlist = [4, 32, 6, 76, 12, 37, 52]
square = [] # Create an empty list 'square' to store results
for sol in numlist: # Iterate over each element in the list 'numlist'
square.append(sol ** 2) # Square each element and append to 'square'
print(square)
Output:
[16, 1024, 36, 5776, 144, 1369, 2704]
Explanation:
In the above example, we created a list named numlist containing numbers, and an empty list named square to store the results. We used a for loop to iterate through each element in the numlist and squared each number using sol ** 2. After that, we appended the result to the square list.
2) Using List Comprehension:
In this example, we are using the List Comprehension method to create a new list:
Example
#created a list
numlist = [4, 32, 6, 76, 12, 37, 52]
#making a new list with condition
square = [sol ** 2 for sol in numlist]
print(square)
Output:
[16, 1024, 36, 5776, 144, 1369, 2704]
Explanation:
In the above example, we used List comprehension to create a new list named ‘square’ by squaring each element from the original list named ‘numlist’.
As we can clearly see, the difference between a ‘for’ loop and list comprehension is evident. The ‘for’ loop requires three lines of code, whereas the list comprehension requires only one line of code.
Conditional Statements in List Comprehension
Conditional Statements in List Comprehension allow the use of if-else to determine or extract items based on specific conditions.
Different Examples for Conditional Statements in Python List Comprehension
Let’s see some examples to understand the use of conditional statements in Python List Comprehension:
Example 1:
In this example, we will create a list of the elements from the given list if the element consists of the character ‘a’.
Example
# given list
car = ['tata', 'honda', 'toyota', 'hyundai', 'skoda', 'suzuki', 'mahindra', 'BMW', 'Mercedes']
# making a new list with condition
new_carlist = [x for x in car if 'a' in x]
# printing new list
print(new_carlist)
Output:
['tata', 'honda', 'toyota', 'hyundai', 'skoda', 'mahindra']
Explanation:
In the above example, we are given a list named ‘car’. We used the list comprehension to create a new list called new_carlist with a condition that extracts only those car names that contain the character ‘a’.
Example 2:
In the following example, we will create a list of cars that do not include the Indian car brands.
Example
car = ['tata', 'honda', 'toyota', 'hyundai', 'skoda','suzuki', 'mahindra', 'BMW', 'Mercedes']
indian_cars = ['tata', 'mahindra']
new_carlist = [x for x in car if x not in indian_cars]
print(new_carlist)
Output:
['honda', 'toyota', 'hyundai', 'skoda', 'suzuki', 'BMW', 'Mercedes']
Explanation:
Here, we used list comprehension to exclude the Indian cars from the car list. The condition ‘if x not in indian_cars’ filters out any car that is listed in ‘indian_cars’ and adds only the non-Indian cars to ‘new_carlist’.
List Comprehension with String
The List Comprehension also helps us to extract elements from a string.
Python List Comprehension Example with String
Let us take an example to demonstrate the list comprehension with string in Python.
Example
name = "Learn python app"
vowels = "aeiou"
# find vowel in the string "Learn python app"
solution = [letter for letter in name if letter in vowels]
print(solution)
Output:
['e', 'a', 'o', 'a']
Explanation:
In the above example, we used list comprehension to iterate through the string “TpointTech” and extract all the vowels. The ‘solution’ stores the result.
Creating a List from range()
When we use the range() function in list comprehension, it helps us to print a large list of numbers.
Python List Comprehension Example using range() Function
Let’s consider an example to demonstrate how to create a list using the range() function in Python.
Example
# Creating a list of numbers from 0 to 5
num = [x for x in range(5)]
print(num)
Output:
[0, 1, 2, 3, 4]
Explanation:
In this example, we have created a list containing numbers using list comprehension. Here, we can see that we have used the range() function to return a range object consisting of the value from 0 to 4.
Creating a List using Nested Loops
We can also create a list using nested loops in List Comprehension. Let us take a look at an example showing how to create a list using nested loop:
Example
#printing cubes of the given range
#List comprehension using nested loop
cubes = [(i, i**3) for i in range(1, 6)]
print(cubes)
Output:
[(1, 1), (2, 8), (3, 27), (4, 64), (5, 125)]
Explanation:
In the above example, we created a nested for loop, i in range(1,6) gives values from 1 to 5 and for each i, we calculate its cube using i**3. The List comprehension stores the output in pairs.
Flattening a list of lists
In Python, we can use list comprehension to flatten a nested list (or list of lists). Flattening a list means converting a nested list into a single list with all its elements.
Python List Comprehension Example for Flattening a list of lists
Let us take an example to demonstrate how to flatten a list of lists in Python list comprehension.
Example
#given list
lists = [[3, 56, 21], [59, 15, 36], [27, 88, 69]]
#using the list comprehension method
solution = [x for y in lists for x in y]
print(solution)
Output:
[3, 56, 21, 59, 15, 36, 27, 88, 69]
Explanation:
- for y in list: It iterates over each sublist [1,2,3] and then [4,5,6]
- for x in y: It iterates over each element in that sublist.
- x: adds each element into the flattened list
Conclusion
Python List Comprehension is an effective method for extracting items from a list and creating a new list based on specific conditions.
List Comprehension can be used with:
- String (character filtering or transformation)
- Nested Loops (for matrix operations or combinations)
- Flattening of List
- range() method (to generate sequences)
- Conditional Statements (to filter or modify data)
List comprehension isn’t limited to these examples and applications; one must try to explore the depth of this topic.
Leave a Reply