# Python Program to count even and odd numbers in a list

Summary:This is a simple python program to count even and odd numbers in a list using for loop, lambda function and list comprehension feature of Python.

Problem Statement: Write a python program to count even and odd numbers in a list.

Description: Given input a list, we need to count the number of even numbers and odd numbers it contains.

## Count Even and Odd Numbers in List Using for Loop

``````numbers = [2, 3, 4, 7, 9, 11, 12, 14, 15]

even = 0
odd = 0
for i in numbers:
if(i % 2 == 0):
even += 1
else:
odd += 1
print("{0} even numbes and {1} odds.".format(even, odd))``````

The above program will give the following output.

``4 even numbes and 5 odds.``

Explanation: The logic of the program is simple, we iterate through each element of the list and check its remainder after dividing by 2. If the remainder is 0 then it is even number so we increment even counter, else it is an odd number and we increment the odd counter.

Finally, we print the output using the format method.

## Count Even and Odd in List Using Lambda Function

``````numbers = [2, 3, 4, 7, 9, 11, 12, 14, 15]

even_list = list(filter(lambda x: (x % 2 == 0), numbers))

odd_list = list(filter(lambda x: (x % 2 != 0), numbers))

print("{0} even numbes and {1} odds.".format(len(even_list), len(odd_list)))``````

The above code will give the following output.

``4 even numbes and 5 odds.``

Explanation: Lambda functions are anonymous functions which are kind of instant run functions. In this code, we create two separate even and odd list using filter function count the length of each.

## Count Even and Odd Numbers in List using List Comprehension

``````numbers = [2, 3, 4, 7, 9, 11, 12, 14, 15]

even_list = [i for i in numbers if (i%2==0)]

print("{0} even numbes and {1} odds.".format(len(even_list), len(numbers)-len(even_list)))``````

The above code will result in the following output.

``4 even numbes and 5 odds.``

Explanation: List comprehension in python is a very useful feature. It helps us to write clean and logical code.

Here in this code we iterate through the `numbers` list and create a new list be selecting only even elements. At last, we count the elements to get even numbers in the list and calculate odd numbers count.