Mastering Python List Comprehension with Conditional Logic
Python's list comprehension is a powerful tool for creating and manipulating lists in a concise and efficient manner. When combined with conditional logic, it becomes even more versatile, allowing you to filter, transform, and manipulate lists based on specific conditions. Let's dive into the world of Python list comprehension with if statements.
Understanding List Comprehension
Before we delve into conditional logic, let's ensure we have a solid understanding of list comprehension. At its core, list comprehension is a compact way of creating lists based on existing lists or other iterable objects. It follows this basic syntax:
new_list = [expression for item in iterable if condition]

The expression is evaluated for each item in the iterable, and if the condition is true, the result of the expression is added to the new list.
Introducing Conditional Logic: If
Now, let's introduce the if statement into our list comprehension. The if statement allows us to add an extra layer of control, enabling us to filter items based on specific conditions. Here's the updated syntax:
new_list = [expression for item in iterable if condition1 and/or condition2]

In this syntax, each condition is evaluated for each item in the iterable. If all conditions are true, the expression is evaluated, and the result is added to the new list.
Examples of List Comprehension with If
Filtering a List
Let's start with a simple example. Suppose we have a list of numbers and we want to create a new list that only includes the even numbers. We can achieve this using list comprehension with an if statement:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

even_numbers = [num for num in numbers if num % 2 == 0]
The resulting list, even_numbers, will contain only the even numbers from the original list.
Transforming a List
List comprehension with if can also be used to transform lists. For instance, let's say we want to create a new list that contains the square of each number in a given list, but only if the number is positive:
numbers = [-2, -1, 0, 1, 2, 3, 4, 5]
squared_positives = [num ** 2 for num in numbers if num > 0]
The resulting list, squared_positives, will contain the squares of the positive numbers from the original list.
Multiple Conditions
We can also use multiple conditions in our list comprehension. Let's create a new list that contains the square of each number in a given list, but only if the number is both positive and even:
numbers = [-2, -1, 0, 1, 2, 3, 4, 5]
squared_positives_evens = [num ** 2 for num in numbers if num > 0 and num % 2 == 0]
The resulting list, squared_positives_evens, will contain the squares of the positive even numbers from the original list.
Nesting List Comprehension with If
List comprehension with if can also be nested, allowing us to create lists based on multiple levels of conditions. For example, let's create a list of lists that contains the squares of the positive even numbers from each sublist in a given list of lists:
lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
squared_evens = [[num ** 2 for num in sublist if num > 0 and num % 2 == 0] for sublist in lists]
The resulting list, squared_evens, will be a list of lists, containing the squares of the positive even numbers from each sublist in the original list.
Conclusion
Python's list comprehension with if statements is a powerful tool for creating and manipulating lists based on specific conditions. By mastering this feature, you can write more concise, efficient, and readable code. Whether you're filtering, transforming, or nesting lists, list comprehension with if provides a flexible and expressive way to achieve your goals.






















