Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program To Find The Largest Element In A Dictionary
Dictionaries are used to store data values in key:value pairs like maps. Unlike other data types that hold only a single value as an element, dictionaries provide key:value pairs to make data access more effective.
Dictionary keys must be unique, so no duplicate keys are allowed. Dictionary items are ordered, changeable, and immutable. Changeable means we can add or remove items after the dictionary is created.
In this article, we will learn how to find the largest element in a dictionary using different methods like loops, sorted(), max() function, and comparison operators.
Methods to Find the Largest Element
There are four main methods to find the largest element in a dictionary:
Using for loop with comparison
Using
sorted()functionUsing
max()functionUsing
operator.itemgetter()
Using For Loop with Comparison
A for loop can iterate through dictionary values to find the largest element by comparing each value.
Example
Here we iterate through dictionary values to find the maximum element:
countries = {"africa": 12, "america": 9, "dubai": 4, "india": 13}
largest = 0
for value in countries.values():
if value > largest:
largest = value
print("Largest element is:", largest)
The output of the above code is ?
Largest element is: 13
Using sorted() Method
The sorted() function sorts dictionary values in ascending order by default. To get the largest value, we access the last element using index [-1].
Example
Here we use sorted() to find the largest element:
scores = {"alina": 93, "steve": 63, "mike": 76, "robin": 89}
largest_score = sorted(scores.values())[-1]
print("Largest score:", largest_score)
# For finding smallest from unique values
subjects = {"hindi": 20, "english": 12, "maths": 20, "science": 10}
smallest_unique = sorted(set(subjects.values()))[0]
print("Smallest unique score:", smallest_unique)
The output of the above code is ?
Largest score: 93 Smallest unique score: 10
Using max() Function
The max() function returns the highest value from an iterable. We can use it directly on dictionary values or combine it with zip() to get the corresponding key.
Example
Here we use max() to find the key with the largest value:
scores = {"alina": 93, "steve": 63, "mike": 76, "robin": 89}
# Get the key with maximum value
max_key = max(zip(scores.values(), scores.keys()))[1]
print("Person with highest score:", max_key)
# Alternative method using key parameter
max_key_alt = max(scores, key=scores.get)
print("Alternative method result:", max_key_alt)
The output of the above code is ?
Person with highest score: alina Alternative method result: alina
Using operator.itemgetter()
The operator.itemgetter() function can be used with max() to find the item with the largest value based on a specific index.
Example
Here we use operator.itemgetter() to find the key with the largest value:
import operator
car_prices = {"audi": 100, "bmw": 1292, "jaguar": 210000, "hyundai": 89}
most_expensive = max(car_prices.items(), key=operator.itemgetter(1))[0]
print("Most expensive car:", most_expensive)
The output of the above code is ?
Most expensive car: jaguar
Comparison of Methods
| Method | Returns | Best For |
|---|---|---|
| For loop | Value only | Custom logic |
sorted() |
Value only | When you need sorted list |
max() |
Key or value | Simple and efficient |
operator.itemgetter() |
Key with max value | Complex data structures |
Conclusion
Use max() with key=dict.get for the most efficient approach. Use operator.itemgetter() for complex scenarios involving tuples or nested structures.
