89DEVs

Python: How to select from list by condition?

How to select from list by condition in Python?

To select from list by condition in Python, use list comprehension or the filter() function. A third way of solving this problem, is the use of loops as shown in example 3. However, this is essentially just a longer way to write a list comprehension.

solution 1: list comprehension

List comprehension can be used to apply rules to a list and select specific elements from a list by conditions. In the example shown below, only even numbers are selected from the list. This is achieved by using the modulo operator. By definition a number is even, if it's divided by 2 and the remainder is 0. The list comprehension below checks for the remainder and adds an element to the evenList only if the remainder is 0. # select from list by condition with list comprehension myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] evenList = [num for num in myList if num % 2 == 0] print(evenList) In this example, myList is populated with integers between 1 and 10. Then, list comprehension is applied to check the remainder of the division by 2. If the remainder is 0, the element is added to the list. Finally, the evenList is printed and the result is shown below. [2, 4, 6, 8, 10]

solution 2: filter() function

Use the built-in filter() function together with a lambda function and the list() function. # select from list by condition with filter() function myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] evenList = list(filter(lambda num: num % 2 == 0, myList)) print(evenList) In this example, myList is populated with integers between 1 and 10 - similar to solution 1. Then a lambda function is used to check if the remainder of the division by 2 is 0. If the remainder is 0, the element passes the filter. The filter() functions returns a filter object. The list() function is used, to turn the filter object back into a list. [2, 4, 6, 8, 10]

solution 3: loops

A third solution to the problem of selecting from a list by conditions is the use of loops. It is the same principle that is used in the solution 1 list comprehension. It is however a longer way to write the same functionality and therefore considered less pythonic. The use of loops to select from a list by condition is shown below for comparison. # select from list by using loops myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] evenList = [] for num in myList: if num % 2 == 0: evenList.append(num) print(evenList) In this example, myList is populated with integers between 1 and 10 - similar to the solutions shown above. Then, an empty list named evenList is defined. This list will be used to store the even elements of myList. Then, a for loop is used to iterate through elements of myList. If an element is divided by 2 and the remainder is 0 it means the element is even. Even elements are appended to the evenList within the if statement. Finally, the evenList is printed and the result is shown below. [2, 4, 6, 8, 10]

                
        

Summary


Click to jump to section