Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
List comprehensions in Python provide a concise way to create a new list by applying an expression to each item in an existing list or other iterable. They can be used to create lists in a more readable and efficient way compared to using for
loops or the map()
function. This tutorial will show you how to use list comprehensions in Python.
Basic list comprehension syntax:
The general syntax for a list comprehension is:
new_list = [expression for item in iterable if condition]
expression
: The operation you want to apply to each item in the iterable.item
: A variable used to represent each item in the iterable.iterable
: An iterable object (e.g., list, tuple, range, or string) whose items you want to process.condition
(optional): A filter that selects items from the iterable based on a condition.Examples of list comprehensions:
squares = [x**2 for x in range(1, 11)] print(squares) # Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
even_numbers = [x for x in range(1, 21) if x % 2 == 0] print(even_numbers) # Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
celsius = [0, 10, 20, 30, 40, 50] fahrenheit = [(9/5)*temp + 32 for temp in celsius] print(fahrenheit) # Output: [32.0, 50.0, 68.0, 86.0, 104.0, 122.0]
words = ['Apple', 'BaNaNa', 'Cherry', 'ORANGE'] lowercase_words = [word.lower() for word in words] print(lowercase_words) # Output: ['apple', 'banana', 'cherry', 'orange']
Nested list comprehensions:
You can use nested list comprehensions to create a list of lists or to flatten a list of lists into a single list.
multiplication_table = [[i * j for j in range(1, 11)] for i in range(1, 11)] for row in multiplication_table: print(row)
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flat_list = [item for sublist in nested_list for item in sublist] print(flat_list) # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
In the example above, the first for
loop iterates over the sublists, and the second for
loop iterates over the items within each sublist.
How to Use List Comprehensions in Python:
# Example squares = [x**2 for x in range(1, 6)]
Creating Lists with Comprehension in Python:
# Example even_numbers = [x for x in range(10) if x % 2 == 0]
Filtering and Transforming Data with List Comprehension:
# Example squared_even_numbers = [x**2 for x in range(10) if x % 2 == 0]
Advanced Techniques for List Comprehension in Python:
# Example mixed_data = [str(x) if x % 2 == 0 else x for x in range(5)]
Nested List Comprehensions in Python:
# Example matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flattened_list = [num for row in matrix for num in row]
List Comprehension vs Traditional For Loop in Python:
# Example with for loop squares = [] for x in range(1, 6): squares.append(x**2) # Example with list comprehension squares = [x**2 for x in range(1, 6)]