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 - Maximum record value key in dictionary
Next article icon

Minimum Value Keys in Dictionary – Python

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a dictionary and need to find all the keys that have the minimum value among all key-value pairs. The goal is to identify the smallest value in the dictionary and then collect every key that matches it. For example, in {‘a’: 3, ‘b’: 1, ‘c’: 2, ‘d’: 1}, the minimum value is 1, so the result should be [‘b’, ‘d’]. Let’s explore different methods to achieve this.

Using min()

In this method, we first find the minimum value in the dictionary using the min() function. Once we have that value, we loop through the dictionary and collect all keys that have this minimum value. The logic is straightforward to find the smallest value, then retrieve the keys that match it.

Python
d = {'Gfg': 11, 'for': 2, 'CS': 11, 'geeks': 8, 'nerd': 2} min_val = min(d.values())  res = [k for k, v in d.items() if v == min_val] print(res) 

Output
['for', 'nerd'] 

Explanation:

  • min(d.values()) finds the minimum value among the dictionary values.
  • List comprehension collects keys where the value equals the minimum value.

Using defaultdict

Here, we use a defaultdict to group all dictionary keys based on their values. As we loop through each key-value pair, we add the key to a list under its corresponding value. Once everything is grouped, we simply pick the list of keys that belong to the smallest value group. This method is especially useful if you’re also interested in organizing or accessing keys by their values later.

Python
from collections import defaultdict  d = {'Gfg' : 1, 'for' : 2, 'CS' : 1} g = defaultdict(list)  for k, v in d.items():     g[v].append(k)  print(g[min(g)]) 

Output
['Gfg', 'CS'] 

Explanation:

  • defaultdict(list) initializes a defaultdict where each value is a list.
  • for k, v in d.items() iterates through the dictionary d with key-value pairs.
  • g[v].append(k) appends the key k to the list of values corresponding to v in the defaultdict.

Using itemgetter with groupby

This approach involves sorting the dictionary items by their values first. After sorting, we use groupby to group keys that have the same value. The first group that appears after sorting will always have the minimum value, so we just take the keys from that group. This method is useful when you also care about sorted order or need structured grouping of keys.

Python
from itertools import groupby from operator import itemgetter  d = {'Gfg': 11, 'for': 2, 'CS': 11, 'geeks': 8, 'nerd': 2} a = sorted(d.items(), key=itemgetter(1))  res = [k for k, _ in groupby(a, key=itemgetter(1))][0] res = [k for k, v in a if v == a[0][1]] print(res) 

Output
['for', 'nerd'] 

Explanation:

  • sorted(d.items(), key=itemgetter(1)) sorts dictionary items by value.
  • groupby(a, key=itemgetter(1)) groups sorted items by their values.
  • [k for k, _ in groupby(a, key=itemgetter(1))][0] extracts the first group of keys (with the minimum value).
  • [k for k, v in a if v == a[0][1]] selects all keys with the minimum value.

Using loop

This is a more manual method where we loop through the dictionary while keeping track of the smallest value seen so far. If we find a value smaller than our current minimum, we reset our result list with that key. If the value matches the current minimum, we add the key to our list. This method gives you full control and doesn’t rely on any built-in functions or imports.

Python
d = {'Gfg' : 1, 'for' : 2, 'CS' : 1} min_val = float('inf') res = []   for k, v in d.items():     if v < min_val:         min_val = v         res = [k]     elif v == min_val:         res.append(k)  print(res) 

Output
['Gfg', 'CS'] 

Explanation:

  • float(‘inf’) initializes min_val to infinity to ensure that the first value encountered will be smaller.
  • for k, v in d.items() iterates through the dictionary items (key-value pairs).
  • if v < min_val checks if the current value is smaller than min_val if true, updates min_val and resets res to [k].
  • elif v == min_val if the current value equals min_val, appends the key k to res.


Next Article
Python - Maximum record value key in dictionary
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python dictionary-programs
Practice Tags :
  • python

Similar Reads

  • Python - Maximum record value key in dictionary
    Sometimes, while working with dictionary records, we can have a problem in which we need to find the key with maximum value of a particular key of nested records in list. This can have applications in domains such as web development and Machine Learning. Lets discuss certain ways in which this task
    6 min read
  • Python - Minimum value pairing for dictionary keys
    Given two lists, key and value, construct a dictionary, which chooses minimum values in case of similar key value pairing. Input : test_list1 = [4, 7, 4, 8], test_list2 = [5, 7, 2, 9] Output : {8: 9, 7: 7, 4: 2} Explanation : For 4, there are 2 options, 5 and 2, 2 being smallest is paired. Input : t
    7 min read
  • Python | Increment value in dictionary
    Sometimes, while working with dictionaries, we can have a use-case in which we require to increment a particular key's value in dictionary. It may seem quite straight forward problem, but catch comes when the existence of a key is not known, hence becomes a 2 step process at times. Let's discuss cer
    6 min read
  • Python Print Dictionary Keys and Values
    When working with dictionaries, it's essential to be able to print their keys and values for better understanding and debugging. In this article, we'll explore different methods to Print Dictionary Keys and Values. Example: Using print() Method [GFGTABS] Python my_dict = {'a': 1, 'b'
    2 min read
  • Inverse Dictionary Values List - Python
    We are given a dictionary and the task is to create a new dictionary where each element of the value lists becomes a key and the original keys are grouped as lists of values for these new keys.For example: dict = {1: [2, 3], 2: [3], 3: [1]} then output will be {2: [1], 3: [1, 2], 1: [3]} Using defau
    2 min read
  • Python - Mapping Key Values to Dictionary
    We are given two list and we need to map key to values of another list so that it becomes dictionary. For example, we are given two list k = ['a', 'b', 'c'] and v = [1, 2, 3] we need to map the keys of list k to the values of list v so that the resultant output should be {'a': 1, 'b': 2, 'c': 3}. Us
    3 min read
  • Python Iterate Dictionary Key, Value
    In Python, a Dictionary is a data structure that stores the data in the form of key-value pairs. It is a mutable (which means once created we modify or update its value later on) and unordered data structure in Python. There is a thing to keep in mind while creating a dictionary every key in the dic
    3 min read
  • Python - Value limits to keys in Dictionaries List
    Given a Dictionaries list, write a Python program to assign limits to each key in dictionary list.( each having all keys ). Examples: Input : test_list = [{"gfg" : 4, "is" : 7, "best" : 10}, {"gfg" : 2, "is" : 5, "best" : 9}, {"gfg" : 1, "is" : 2, "best" : 6}] Output : {'gfg': [1, 4], 'is': [2, 7],
    11 min read
  • Dictionary items in value range in Python
    In this article, we will explore different methods to extract dictionary items within a specific value range. The simplest approach involves using a loop. Using LoopThe idea is to iterate through dictionary using loop (for loop) and check each value against the given range and storing matching items
    2 min read
  • Python - Dictionary Values Division
    Sometimes, while working with dictionaries, we might have utility problem in which we need to perform elementary operation among the common keys of dictionaries. This can be extended to any operation to be performed. Let’s discuss division of like key values and ways to solve it in this article. Met
    5 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