Python one of the most used coding languages has some very powerful features that it can offer. One feature that helps developers is List Comprehension. List comprehension is a clean and concise way to create lists in Python. But, what exactly is list comprehension. To sum it up, list comprehension is a compact syntax for creating lists. You can create these lists based on sequences, existing lists, or really anything iterable in Python. This will often replace the need for 'for' loops. Lets jump into the syntax and how to use list comprehension.
The basic syntax for a list comprehension is:
new_list = [expression for item in iterable if condition]
expression: is the operation one wants to perform on each item in the iterable.
item: is the variable that takes each value from the iterable one by one.
condition: is an optional part of the list comprehension. This filters items that meet a specific condition.
Lets look at some simple examples to get started.
Example 1: Filtering Odd Numbers
Say there is a list of numbers, one wants to create a new list that only contains the odd numbers. Here is the tradition 'for' loop:
numbers = [1, 2, 3, 4, 5]
odd_numbers = []
for num in numbers:
if num % 2 != 0:
odd_numbers.append(num)
print (odd_numbers)
#Output: [1, 3, 5]
Now with list comprehension:
numbers = [1, 2, 3, 4, 5]
odd_numbers = [num for num in numbers if num % 2 != 0]
print(odd_numbers)
#Output: [1, 3, 5]
Example 2: Squaring Numbers
Consider a list of numbers, one wants to create a new list that has all of the squares of the original numbers. Again one could use a 'for' loop:
numbers = [1, 2, 3, 4, 5]
squared_numbers = []
for num in numbers:
squared_numbers.append(num ** 2)
print(squared_numbers)
#Output: [1, 4, 9, 16, 25]
Now with list comprehension:
numbers = [1, 2, 3, 4, 5]
squared_numbers = [num ** 2 for num in numbers]
print(squared_numbers)
#Output: [1, 4, 9, 16, 25]
List Comprehension with Conditional Expressions
Python list comprehensions can also have conditional expressions, which makes them a lot more versatile. Here is the syntax for conditional expression:
value_if_true if condition else value_if_false
Example 3: Mapping Odd and Even
Say one wants to create a lists that maps odd numbers to the string 'odd' and even numbers to the string 'even':
numbers = [1, 2, 3, 4, 5]
mapped_numbers = ['odd' if num % 2 != 0 else 'even' for num in numbers]
print(mapped_numbers)
#Output: ['odd', 'even', 'odd', 'even', 'odd']
Nested List Comprehension
List comprehensions can even be nested! This allows you to make more complex structures, such as 2D lists:
Example 4: Creating a 3X3 Identity Matrix
identity_matrix = [[1 if row == col else 0 for col in range(3)] for row in range(3)]
print(identity_matrix)
#Output: [[1, 0, 0], [0, 1, 0] [0, 0, 1]]
Python list comprehension is a very powerful and efficient tool that really simplifies list creation and manipulation. By using list comprehension one can write more expressive and efficient code. Those who embrace list comprehension find that their code becomes more readable and concise than ever before.
Top comments (0)