w3resource

Python: Count the even, odd numbers in a given array of integers using Lambda


13. Even/Odd Count Lambda

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

For more Practice: Solve these Related Problems:

  • Write a Python program to count the number of prime and composite numbers in a given list using lambda.
  • Write a Python program to count numbers divisible by 4 versus those not divisible by 4 in a list using lambda.
  • Write a Python program to count the occurrences of positive and negative numbers separately in a list using lambda.
  • Write a Python program to count the frequency of even numbers, odd numbers, and zeros in a list using lambda.

Go to:


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.

Python Code Editor:

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.