Python program to count Even and Odd numbers in a Dictionary
Last Updated :
10 Apr, 2023
Given a python dictionary, the task is to count even and odd numbers present in the dictionary.
Examples:
Input : {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e' : 5}
Output : Even = 2, odd = 3
Input : {'x': 4, 'y':9, 'z':16}
Output : Even = 2, odd = 1
Approach using values() Function: Traverse the dictionary and extract its elements using values() function and for each extracted value, check if it is even or odd. Finally, print the respective counts.
Python3
# Python3 Program to count even and
# odd numbers present in a dictionary
# Function to count even and odd
# numbers present in a dictionary
def countEvenOdd(dict):
# Stores count of even
# and odd elements
even = 0
odd = 0
# Traverse the dictionary
for i in dict.values():
if i % 2 == 0:
even = even + 1
else:
odd = odd + 1
print("Even Count: ", even)
print("Odd Count: ", odd)
# Driver Code
dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
countEvenOdd(dict)
Output:Even Count: 2
Odd Count: 3
Time Complexity: O(N), where N is the length of dict
Auxiliary Space: O(1)
Alternate Approach: Iterate over each item in the dictionary, and for each element, check if it is even or odd. Finally, print the respective counts.
Python3
# Python3 Program to count even
# and odd elements in a dictionary
# Function to count even and
# odd elements in a dictionary
def countEvenOdd(dict):
even = 0
odd = 0
# Iterate over the dictionary
for i in dict:
# If current element is even
if dict[i] % 2 == 0:
# Increase count of even
even = even + 1
# Otherwise
else:
# Increase count of odd
odd = odd + 1
print("Even count: ", even)
print("Odd count: ", odd)
# Driver Code
# Given Dictionary
dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
countEvenOdd(dict)
Output:Even count: 2
Odd count: 3
Time Complexity: O(N), where N is the length of dict
Auxiliary Space: O(1)
Approach : Using bitwise & operator
Python3
# Python3 Program to count even
# and odd elements in a dictionary
#using bitwise operator
def fun1(x):
if(x&1!=0): # Using bitwise opearor
return x
# Given Dictionary
dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
x=list(dict.values())
y=filter(fun1,x)
odd=len(list(y))
even=len(x)-odd
print("Even count: ", even)
print("Odd count: ", odd)
#This code contributed by vinay pinjala.
OutputEven count: 2
Odd count: 3
Time Complexity: O(N), where N is the length of dict
Auxiliary Space: O(N)
Approach : Using values(),filter() and len() methods
Python3
# Python3 Program to count even
# and odd elements in a dictionary
def fun1(x):
if(x%2==0):
return x
# Given Dictionary
dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
x=list(dict.values())
y=filter(fun1,x)
even=len(list(y))
odd=len(x)-even
print("Even count: ", even)
print("Odd count: ", odd)
OutputEven count: 2
Odd count: 3
Time Complexity: O(N), where N is the length of dict
Auxiliary Space: O(N)
Similar Reads
Python Program for Check if count of divisors is even or odd Write a Python program for a given number ânâ, the task is to find its total number of divisors that are even or odd. Examples: Input : n = 10 Output: Even Input: n = 100Output: Odd Input: n = 125Output: Even Python Program for Check if count of divisors is even or odd using Naive Approach:A naive a
4 min read
Python | Count number of items in a dictionary value that is a list In Python, dictionary is a collection which is unordered, changeable and indexed. Dictionaries are written with curly brackets, and they have keys and values. It is used to hash a particular key. A dictionary has multiple key:value pairs. There can be multiple pairs where value corresponding to a ke
5 min read
Python Program for Number of elements with odd factors in given range Given a range [n,m], find the number of elements that have odd number of factors in the given range (n and m inclusive). Examples: Input : n = 5, m = 100 Output : 8 The numbers with odd factors are 9, 16, 25, 36, 49, 64, 81 and 100 Input : n = 8, m = 65 Output : 6 Input : n = 10, m = 23500 Output :
2 min read
Use get() method to Create a Dictionary in Python from a List of Elements We are given a list of elements and our task is to create a dictionary where each element serves as a key. If a key appears multiple times then we need to count its occurrences. get() method in Python helps achieve this efficiently by providing a default value when accessing dictionary keys. For exa
1 min read
Counting Word Frequency and Making a Dictionary from it We need to count how often each word appears in a given text and store these counts in a dictionary. For instance, if the input string is "Python with Python gfg with Python", we want the output to be {'Python': 3, 'with': 2, 'gfg': 1}. Each key in the dictionary represents a unique word, and the co
3 min read