Python: Count the even, odd numbers in a given array of integers using Lambda
Python Lambda: Exercise-13 with Solution
Write a Python program to count the even and odd numbers in a given array of integers using Lambda.
Sample Solution:
Python Code :
# Create a list 'array_nums' containing integers
array_nums = [1, 2, 3, 5, 7, 8, 9, 10]
# Display a message indicating that the following output will show the original array
print("Original arrays:")
print(array_nums) # Print the contents of 'array_nums'
# Use the 'filter()' function with lambda functions to filter and count the number of odd and even numbers
# Use 'filter()' with a lambda function to filter and count the number of odd numbers in 'array_nums'
odd_ctr = len(list(filter(lambda x: (x % 2 != 0), array_nums)))
# Use 'filter()' with a lambda function to filter and count the number of even numbers in 'array_nums'
even_ctr = len(list(filter(lambda x: (x % 2 == 0), array_nums)))
# Display the number of even and odd numbers in the original array
print("\nNumber of even numbers in the above array: ", even_ctr)
print("\nNumber of odd numbers in the above array: ", odd_ctr)
Sample Output:
Original arrays: [1, 2, 3, 5, 7, 8, 9, 10] Number of even numbers in the above array: 3 Number of odd numbers in the above array: 5
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to rearrange positive and negative numbers in a given array using Lambda.
Next: Write a Python program to find the values of length six in a given list using Lambda.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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://w3resource.com/python-exercises/lambda/python-lambda-exercise-13.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics