In this article, you’ll learn about list comprehension.
Table of Contents
What is List Comprehension?
List comprehension is basically creating lists based on existing iterables. It can also be described as representing for and if loops with a simpler and more appealing syntax.
Syntax:
newList = [ expression(element) for element in oldList if condition ]
For Loop vs List Comprehensions
There are various ways to iterate through a list. However, the most common approach is to use the for loop.
For Loop
# Empty list
List = []
# Traditional approach of iterating
for character in 'Techarge!':
List.append(character)
# Display list
print(List)
Code language: PHP (php)
List Comprehension
# Using list comprehension to iterate through loop
List = [character for character in 'Techarge!']
# Displaying list
print(List)
Code language: PHP (php)
Output the both the case are same,
[‘T’, ‘e’, ‘c’, ‘h’, ‘a’, ‘r ‘, ‘g’, ‘e ‘, ‘!’]
Advantages of List Comprehension
- More time efficient and space efficient than loops.
- Require fewer lines of code.
- Transforms iterative statement into a formula.
Nested List Comprehensions
Nested List Comprehensions are nothing but a list comprehension within another list comprehension which is quite similar to nested for loops. Below is the program which implements nested loop:
# Nested list comprehension
matrix = [[j for j in range(5)] for i in range(3)]
print(matrix)
Code language: PHP (php)
Output:
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
List Comprehensions and Lambda
Lambda Expressions are nothing but shorthand representations of Python functions. Using list comprehensions with lambda creates an efficient combination.
Let us look at the below examples:
Using Loops
# using lambda to print table of 5
numbers = []
for i in range(1, 10):
numbers.append(i*5)
print(numbers)
Code language: PHP (php)
Using List Comprehension
numbers= [i*5 for i in range(1,10)]
print(numbers)
Code language: PHP (php)
Using List Comprehension and Lambda
# using lambda to print table of 5
numbers = list(map(lambda i: i*5, [i for i in range(1,10)]))
print(numbers)
Code language: PHP (php)
Output in all the case are same .
[5,10,15,20,25,30,35,40,45,50,55,60]
Conditionals in List Comprehension
We can also add conditional statements to the list comprehension. We can create a list using range(), operators, etc. and cal also apply some conditions to the list using the if statement.
Let look on some of the examples
Example 1: Display square of even numbers from 1 to 10.
# Getting square of even numbers from 1 to 10
squares = [n**2 for n in range(1, 11) if n%2==0]
# Display square of even numbers
print(squares)
Code language: PHP (php)
Output
[4, 16, 36, 64, 100]
Example 2: Display cube of odd numbers from 1 to 10.
# Getting cube of odd numbers from 1 to 10
squares = [n**3 for n in range(1, 11) if n%3==0]
# Display square of even numbers
print(squares)
Code language: PHP (php)
Output
[1,27,125,343,729]
Example 3: Toggle case of each character in a string.
# Initializing string
string = 'Techarge'
# Toggle case of each character
List = list(map(lambda i: chr(ord(i)^32), string))
# Display list
print(List)
Code language: PHP (php)
Output
['t','E','C','H','A','R','G','E']
Example 4: Reverse each string in a tuple.
# Reverse each string in tuple
List = [string[::-1] for string in ('Code', 'for', 'Good')]
# Display list
print(List)
Code language: PHP (php)
Output
['edoC', 'rof', 'dooG']
Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!