Press "Enter" to skip to content

Python program to filter a list of numbers using Lambda expression

In this post, we will see how to filter the list of numbers. We will filter out the even and odd numbers from the list of numbers. To filter the numbers, we will use the filter() function available in python.

Algorithm
Step 1: Declare a list of numbers
Step 2: Use filter() function with lambda expression to find the even numbers. If the number is divisible by 2 and reminder is 0 then that number is even number.
Step 3: Print the list of even numbers
Step 4: Use filter function with lambda expression to find the odd numbers. If the number is divisible by 2 and reminder is not 0 then that number is odd number.
Step 5: Print the list of odd numbers
Step 6: End
Example

Input
[11, 22, 33, 44, 55, 66, 77, 88, 99, 100]
Output
List of even numbers:
[22, 44, 66, 88, 100]
List Odd numbers:
[11, 33, 55, 77, 99]

Program

numbers = [11, 22, 33, 44, 55, 66, 77, 88, 99, 100]
print("List of numbers:")
print(numbers)
print("\nList of even numbers:")
evenNumbers = list(filter(lambda x: x%2 == 0, numbers))
print(evenNumbers)
print("\nList Odd numbers:")
oddNumbers = list(filter(lambda x: x%2 != 0, numbers))
print(oddNumbers)
Freshers Placement

Output

List of numbers:
[11, 22, 33, 44, 55, 66, 77, 88, 99, 100]

List of even numbers:
[22, 44, 66, 88, 100]

List Odd numbers:
[11, 33, 55, 77, 99]

Comments are closed.