Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Program to find XOR of values of all even keys in a dictionary
Next article icon

Python program to count Even and Odd numbers in a Dictionary

Last Updated : 10 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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.
 
 
Output
Even 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)
 
 
Output
Even count:  2 Odd count:  3

Time Complexity: O(N), where N is the length of dict
Auxiliary Space: O(N)



Next Article
Python Program to find XOR of values of all even keys in a dictionary

T

thotasravya28
Improve
Article Tags :
  • DSA
  • Mathematical
  • Python Programs
  • School Programming
  • Technical Scripter
  • Python dictionary-programs
Practice Tags :
  • Mathematical

Similar Reads

  • Python Program to Count Even and Odd Numbers in a List
    In Python working with lists is a common task and one of the frequent operations is counting how many even and odd numbers are present in a given list. The collections.Counter method is the most efficient for large datasets, followed by the filter() and lambda approach for clean and compact code. Us
    4 min read
  • Python program to count positive and negative numbers in a list
    In this article, we will explore various methods to count positive and negative numbers in a list. The simplest way to do this is by using a loop. This method counts the positive and negative numbers in a list by iterating through each element using for loop. [GFGTABS] Python a = [10, -20, 30, -40,
    2 min read
  • Python Program to Check if a Number is Odd or Even
    Even Numbers are exactly divisible by 2 and Odd Numbers are not exactly divisible by 2. We can use modulo operator (%) to check if the number is even or odd. For even numbers, the remainder when divided by 2 is 0, and for odd numbers, the remainder is 1. In this article, we will learn how to check i
    2 min read
  • Count all Elements in a Nested Python Dictionary
    Nested dictionaries are a common data structure in Python, often used to represent complex relationships and hierarchies. When working with nested dictionaries, you may encounter situations where you need to count all the elements within them. In this article, we will explore some simple and commonl
    3 min read
  • Python Program to find XOR of values of all even keys in a dictionary
    Given a dictionary in Python, our task is to find the XOR of values of all even keys in a dictionary in Python. Note: All the keys and values in the dictionary are integers. Examples: Input : dic= {1:3, 4:5, 6:7, 3 :8}Output : 2Explanation: Even keys in the dictionary are 4,6 and their values are 5,
    5 min read
  • Python | N consecutive Odd or Even Numbers
    The problem focused on in this article is quite specific and may be less useful in different domains. But the way this is going to solve may open doors to solve potentially like problems, hence making it worth a read. This article solves the problem of testing if a list contains a series of odd or e
    5 min read
  • Python Program for Frequencies of even and odd numbers in a matrix
    Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 1
    4 min read
  • Python Program to Print Largest Even and Largest Odd Number in a List
    Auxiliary Given a list. The task is to print the largest even and largest odd number in a list. Examples: Input: 1 3 5 8 6 10 Output: Largest even number is 10 Largest odd number is 5 Input: 123 234 236 694 809 Output: Largest odd number is 809 Largest even number is 694 The first approach uses two
    7 min read
  • Python Program to calculate Dictionaries frequencies
    Given a list of dictionaries, the task here is to write a python program to extract dictionary frequencies of each dictionary. Input : test_list = [{'gfg' : 1, 'is' : 4, 'best' : 9}, {'gfg' : 6, 'is' : 3, 'best' : 8}, {'gfg' : 1, 'is' : 4, 'best' : 9}, {'gfg' : 1, 'is' : 1, 'best' : 9}, {'gfg' : 6,
    6 min read
  • Python program to print even numbers in a list
    Getting even numbers from a list in Python allows you to filter out all numbers that are divisible by 2. For example, given the list a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], you might want to extract the even numbers [2, 4, 6, 8, 10]. There are various efficient methods to extract even numbers from a li
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences