Pages

Thursday, March 11, 2021

Using if-else statements in List Comprehension

List Comprehension

List comprehension is one of the ways of creating list. List would be created as a result of some operation on a range, set or list based on some condition (optional). Click Here to see more about list comprehension. 

In this post, we will see how to use if - else statements in list comprehension. 

Let's have a quick look at using if statement alone before we see if - else statements. 

E.g.: 

Create a list of odd numbers less than 10. 

odd_numbers = [number for number in range(10) if number % 2 != 0]

    In the above example, We are using if statement towards the end for identifying the odd numbers. 

    We are only considering the odd numbers using the if statement and leaving the others. But if we have to to a different operation for even numbers and different operation for odd numbers, we should be using if-else statements. 

    if-else statements in List Comprehension

    So, How do we use else statement in list comprehension? Is it just adding else statement after if condition? 

    Syntax wise it's a little different if we need to use both if and else statements. 

    Let's have a look at a simple example to understand this better. 

    E.g.: 

    Create a list with square of even numbers between 0 and 9 and just add "ODD" for every odd number between 0 and 9.

    This requires both if and else statements.

    result_list = 
    [number * number if number % 2 == 0 else "ODD" for number in range(10)]

    Let's breakdown the above statement into four parts. 
    • number * number indicates the expression that needs to be run if the condition is true and the resulting value is to be added to the list. This can be any expression as per the requirement. 
    • if number % 2 == 0 indicates the required if condition.
    • else "ODD" contains both the else statement and the value that needs to be added if the condition mentioned is False. Value here can be any expression as required. For example, We can use number * number if we need to calculate the square of odd number.
    • for number in range(10) indicates the for loop over an iterable. 

    So, syntax wise if - else statements in list comprehension would look like below.

    [expression if condition else expression for element in iterable]

    Key difference between using is if and if-else is the placement of these statements.
    • if statement is to be used at the end (i.e., after for) if no else statement is required. 
    • if - else statements are to be used at the beginning (i.e., before for) if else statement is required. 

    One thing to note is, List comprehension looks to be the easiest way of creating list. But, this can get more complicated and difficult to read and understand when we use it with multiple if (and else) and for statements. 



    If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

    No comments:

    Post a Comment

    Exception Handling in Python (try - except-else-finally)

    Exception Handling in Python Exception handling is key for any programming language. When an exception occurs Python interpreter stops execu...