What is List Comprehension in Python?
List comprehension is a concise and powerful way to create lists in Python. It allows you to create a new list by applying a transformation or condition to each element of an existing list, sequence, or any other iterable object. This feature makes your code more readable, compact, and efficient compared to traditional for loops.
The basic syntax of list comprehension is as follows:
new_list = [expression for item in iterable if condition]
Here's how it works:
- The
expression
is the operation you want to perform on each item in theiterable
. - The
item
represents the current element being processed from theiterable
. - The
if condition
(optional) is a filter that selects only the items for which the condition is true.
Let's look at some examples to better understand list comprehension:
Example 1: Creating a list of squares
Suppose you want to create a list of squares from the numbers 1 to 10. Using a traditional for loop, the code would look like this:
squares = []
for i in range(1, 11):
squares.append(i**2)
print(squares) # Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
With list comprehension, the same task can be accomplished in a single line:
squares = [i**2 for i in range(1, 11)]
print(squares) # Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
The list comprehension [i**2 for i in range(1, 11)]
creates a new list where each element is the square of the corresponding number in the range 1
to 10
.
Example 2: Filtering a list
Suppose you have a list of numbers and you want to create a new list containing only the even numbers. Using a traditional for loop, the code would look like this:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = []
for num in numbers:
if num % 2 == 0:
even_numbers.append(num)
print(even_numbers) # Output: [2, 4, 6, 8, 10]
With list comprehension, the same task can be accomplished in a single line:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = [num for num in numbers if num % 2 == 0]
print(even_numbers) # Output: [2, 4, 6, 8, 10]
The list comprehension [num for num in numbers if num % 2 == 0]
creates a new list containing only the even numbers from the numbers
list.
Nested List Comprehension
List comprehension can also be nested, allowing you to create complex data structures like lists of lists. Here's an example of creating a 3x3 multiplication table using nested list comprehension:
multiplication_table = [[x*y for x in range(1, 4)] for y in range(1, 4)]
print(multiplication_table)
# Output: [[1, 2, 3], [2, 4, 6], [3, 6, 9]]
The inner list comprehension [x*y for x in range(1, 4)]
creates a list of products for each row, and the outer list comprehension [... for y in range(1, 4)]
creates a list of these rows, resulting in a 3x3 multiplication table.
Mermaid Diagram
Here's a Mermaid diagram that visualizes the structure and key components of list comprehension in Python:
The diagram shows that the list comprehension takes an Iterable
(such as a list, range, or any other iterable object), applies an Expression
to each element, and then filters the elements based on an optional Condition
, resulting in a New List
.
List comprehension is a powerful and concise way to create lists in Python. It can make your code more readable, maintainable, and efficient, especially when dealing with large or complex data structures. By understanding the basic syntax and structure of list comprehension, you can leverage this feature to write more expressive and Pythonic code.