Python List Comprehension Using If-Else Last Updated : 05 Dec, 2024 Comments Improve Suggest changes Like Article Like Report List comprehension with if-else in Python is a concise way to apply conditional logic while creating a new list. It allows users to add elements based on specific conditions and even modify them before adding.Using if-else Inside List ComprehensionThis is the simplest and most efficient way to apply conditional logic directly. This applies the if-else condition directly in the list comprehension. Each number is checked for divisibility by 2, and either "Even" or "Odd" is added to the list. Python a = [1, 2, 3, 4, 5] # Add 'Even' for even numbers, otherwise 'Odd' result = ['Even' if n % 2 == 0 else 'Odd' for n in a] print(result) Output['Odd', 'Even', 'Odd', 'Even', 'Odd'] Let's explore some other methods on how to use list comprehension using if-elseTable of ContentUsing if Condition Only (Without else)Nested if-else in List ComprehensionUsing if Condition Only (Without else)This method is used when elements are added only if the condition is met. Here, the if condition filters out numbers that do not meet the condition. Only even numbers are added to the new list. Python a = [1, 2, 3, 4, 5] # Include only even numbers in the list result = [n for n in a if n % 2 == 0] print(result) Output[2, 4] Nested if-else in List ComprehensionHandle multiple conditions with nested logic. Using Nested if-else in List allows chaining of if-else conditions. It categorizes each number based on whether it is divisible by 2, 3, or neither. Python a = [1, 2, 3, 4, 5] # Categorize numbers based on divisibility result = ['Divisible by 2' if n % 2 == 0 else 'Divisible by 3' if n % 3 == 0 else 'Other' for n in a] print(result) Output['Other', 'Divisible by 2', 'Divisible by 3', 'Divisible by 2', 'Other'] Comment More infoAdvertise with us Next Article Python List Comprehension Using If-Else boora_harsha_vardhan Follow Improve Article Tags : Python Python Programs python-list Practice Tags : pythonpython-list Similar Reads Python List Comprehension With Two Lists List comprehension allows us to generate new lists based on existing ones. Sometimes, we need to work with two lists together, performing operations or combining elements from both. Let's explore a few methods to use list comprehension with two lists.Using zip() with List ComprehensionOne of the mos 3 min read Two For Loops in List Comprehension - Python List comprehension is a concise and readable way to create lists in Python. Using two for loops in list comprehension is a great way to handle nested iterations and generate complex lists. Using two for-loopsThis is the simplest and most efficient way to write two for loops in a list comprehension. 2 min read 4 Tips To Master Python List Comprehensions Python's list comprehensions offer a concise and powerful way to create lists. They allow you to express complex operations in a single line of code, making your code more readable and efficient. In this article, we will explore four tips to master Python list comprehensions with five commonly used 4 min read Python - Test if string contains element from list Testing if string contains an element from list is checking whether any of the individual items in a list appear within a given string.Using any() with a generator expressionany() is the most efficient way to check if any element from the list is present in the list. Pythons = "Python is powerful an 3 min read Python - Check if List contains elements in Range Checking if a list contains elements within a specific range is a common problem. In this article, we will various approaches to test if elements of a list fall within a given range in Python. Let's start with a simple method to Test whether a list contains elements in a range.Using any() Function - 3 min read Square Each Odd Number in a List using List Comprehension The task of squaring each odd number in a list using list comprehension in Python involves iterating through the list, identifying odd numbers, and squaring them while leaving the even numbers unchanged. For example, given a list a = [1, 2, 3, 4, 5, 6, 7], the goal is to square each odd number, resu 2 min read Check if String is Empty or Not - Python We are given a string and our task is to check whether it is empty or not. For example, if the input is "", it should return True (indicating it's empty), and if the input is "hello", it should return False. Let's explore different methods of doing it with example:Using Comparison Operator(==)The si 2 min read Check if any element in list satisfies a condition-Python The task of checking if any element in a list satisfies a condition involves iterating through the list and returning True if at least one element meets the condition otherwise, it returns False. For example, in a = [4, 5, 8, 9, 10, 17], checking ele > 10 returns True as 17 satisfies the conditio 2 min read Convert Each Item in the List to String using Python Converting each item in a list to a string is a common task when working with data in Python. Whether we're dealing with numbers, booleans or other data types, turning everything into a string can help us format or display data properly. We can do this using various methods like loops, the map() fun 3 min read Like