Python's list comprehension is a powerful feature that allows you to create lists in a concise and efficient way. It's a compact way of creating lists based on existing lists or other iterable objects. In this article, we'll delve into the world of Python list comprehension, focusing on the use of 'if' and 'else' statements to filter and manipulate list elements.
Understanding List Comprehension
Before we dive into 'if' and 'else' statements, let's ensure we understand the basics of list comprehension. At its core, list comprehension consists of brackets containing an expression followed by a 'for' statement, then zero or more 'for' or 'if' statements. The expression is executed for each item in the iterable, and the results are collected in a new list.
Syntax of List Comprehension
The general syntax of list comprehension is as follows:

| Syntax | Description |
|---|---|
| [expression for item in iterable if condition] | The expression is evaluated for each item in the iterable, and the result is included in the list if the condition is true. |
Using 'if' in List Comprehension
The 'if' statement in list comprehension allows you to filter items based on a condition. Only items for which the condition is true are included in the new list. Here's an example:
Let's say we have a list of numbers and we want to create a new list that only includes the even numbers.
```python numbers = [1, 2, 3, 4, 5, 6] evens = [num for num in numbers if num % 2 == 0] print(evens) # Output: [2, 4, 6] ```

Using 'if-else' in List Comprehension
The 'if-else' statement in list comprehension allows you to perform different operations based on a condition. Here's how it works:
```python numbers = [1, 2, 3, 4, 5, 6] result = [num * 2 if num % 2 == 0 else num + 3 for num in numbers] print(result) # Output: [4, 5, 6, 8, 7, 9] ```
In this example, if the number is even, it's multiplied by 2. If it's odd, it's increased by 3.

Nested 'if' and 'if-else' Statements
You can also nest 'if' and 'if-else' statements in list comprehension to create more complex conditions. Here's an example:
```python numbers = [1, 2, 3, 4, 5, 6] result = [num * 2 if num % 2 == 0 else num + 3 if num > 3 else num for num in numbers] print(result) # Output: [4, 5, 6, 8, 7, 9] ```
In this case, the 'if-else' statement is nested within another 'if' statement.
Multiple 'for' Loops in List Comprehension
List comprehension also supports multiple 'for' loops, allowing you to iterate over multiple iterables simultaneously. Here's an example:
```python list1 = [1, 2, 3] list2 = [4, 5, 6] result = [a * b for a in list1 for b in list2] print(result) # Output: [4, 5, 6, 8, 10, 12, 12, 15, 18] ```
In this example, the expression 'a * b' is evaluated for each pair of numbers from 'list1' and 'list2'.
List comprehension is a versatile and powerful feature of Python. By mastering 'if' and 'else' statements in list comprehension, you can create concise and efficient code to manipulate and filter lists. Whether you're working with numbers, strings, or complex objects, list comprehension provides a quick and easy way to create new lists based on existing data.






















