w3resource

Python: Filter a list of integers using Lambda

Python Lambda: Exercise-5 with Solution

Write a Python program to filter a list of integers using Lambda.

Sample Solution:

Python Code :

# Create a list of integers named 'nums'
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Display a message indicating that the following output will show the original list of integers
print("Original list of integers:")
print(nums)

# Display a message indicating that the following output will show even numbers from the list
print("\nEven numbers from the said list:")

# Use the 'filter()' function with a lambda function to filter even numbers from 'nums'
# Create a new list 'even_nums' containing only the even numbers from the original list
even_nums = list(filter(lambda x: x % 2 == 0, nums))
print(even_nums)

# Display a message indicating that the following output will show odd numbers from the list
print("\nOdd numbers from the said list:")

# Use the 'filter()' function with a lambda function to filter odd numbers from 'nums'
# Create a new list 'odd_nums' containing only the odd numbers from the original list
odd_nums = list(filter(lambda x: x % 2 != 0, nums))
print(odd_nums) 

Sample Output:

Original list of integers:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Even numbers from the said list:
[2, 4, 6, 8, 10]

Odd numbers from the said list:
[1, 3, 5, 7, 9]

Explanation:

In the exercise above -

  • It initializes a list named 'nums' containing integers from 1 to 10.
  • Prints the original list of integers ('nums') to the console.
  • Filters even and odd numbers from the 'nums' list separately and prints them:
    • It uses the "filter()" function with a lambda function to filter even numbers from the 'nums' list. The lambda function checks if the number is even (x % 2 == 0) and creates a new list called 'even_nums' containing only even numbers.
    • It then prints the list of even numbers ('even_nums').
    • Similarly, it uses the "filter()" function with a lambda function to filter odd numbers from the 'nums' list. The lambda function checks if the number is odd (x % 2 != 0) and creates a new list called 'odd_nums' containing only the odd numbers.
    • It then prints the list of odd numbers ('odd_nums').

Test for a single number:

Python Code :

print((lambda x: (x % 2 and 'Odd number' or 'Even number'))(5))
print((lambda x: (x % 2 and 'Odd number' or 'Even number'))(8))

Sample Output:

Odd number
Even number

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to sort a list of dictionaries using Lambda.
Next: Write a Python program to square and cube every number in a given list of integers using Lambda.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/lambda/python-lambda-exercise-5.php