Python Program To Find The Largest Element In A Dictionary


Dictionaries are used to store data values in key:value pairs like maps (which unlike other data types holds only a single value as an element). key:value is provided in dictionaries to make it more effective.

Keys are unique. Dictionary key must be unique. So, no duplicate values are allowed in dictionaries. Dictionaries items are ordered, changeable, immutable. Changeable means here is that, we can add or remove items after the dictionaries are created.

In this article, we will know about how to find a largest element in a dictionary by using different functions. There are many functions to find a largest element in dictionary by using different method like “for” and “in”, sort() method, max() function etc.

How to Search an Element From a Dictionary?

Here, we have given a dictionary of numbers and we have to find a largest element from a dictionary by using different methods. There four methods to search an element in a dictionary.

  • By using “for” and “in” loop

  • By Using sorted()

  • By using max()

  • By using “==” operator

Using“for” and “in” Loop

A for loop is used to execute a statement repeatedly until given condition is satisfied. And when condition becomes false, the line immediately after the loop in the program is executed.

The “in” operator determines whether a given value is constituent element of a sequence such as a string, array, list, or tuple, dictionary. It is used to search an element in a dictionary. For example −

Example

Here, we have a program in which we have used “for loop” to searching a largest element in a dictionary. In this program, we have to search max value of element that exists in a dictionary. So, output comes as “13”.

names= {"africa ":12, "america":9, "dubai":4, "india":13}
max = max(names.values())
max2= 0
for v in names.values():
   if(v>max2 and v==max):
      max2 = v
print(" largest element is:",max2)

Output

largest element is: 13 

Using Sorted() Method

The sorted() function can be used to sort a dictionary in ascending, descending ,or users defined order. Sorted() method by default gives the sort() value of list in ascending order. So, when we have to give (dict [-1]) while print output in order to find largest value. Sort() function is used to sorted dictionary in order from A-Z, to a-z in the alphabet. For example −

Example

Here, we have a program in which we have used “sorted()” to searching a largest element in a dictionary. In this program, we have to search max value of element by giving reverse = ”true” and value as [-1] that exists in a dictionary. So, output comes as “93 and 10”.

names= {"alina": 93, "steve": 63, "mike": 76, "robin":89}
print("Output1:", sorted(names.values())[-1])
subject = {"hindi": 20, "english": 12, "maths": 20, "science": 10}
print("Output2:", sorted(set(subject.values()), reverse=True)[-1])

Output

Output1: 93
Output2: 10

By using max()

The max() function returns the value of highest elements from list or highest value in an iterable. It is used to find highest value of strings as well numbers. For example −

Example

Here, we have a program in which we have used “max()” to searching a largest element in a dictionary .In this program, we have to search max value of element that exists in a dictionary. So, output comes as “alina”.

names= {"alina": 93, "steve": 63, "mike": 76, "robin":89}
x= max(zip (names.values(),names.keys()))[1]
print("the highest value is", x)

Output

the highest value is alina

Using “= =” Operator

The ”==”operator is used to compare the value or equality of two object. They are also called “Relational operator”. If the value of two operands is equal, then the condition becomes true (a==b). If the value of two operands is not equal, then the condition becomes false (a is not equal to b). Here, we will find largest element in a dictionary by using ”==” operator. For example −

Example

Here, we have a program in which we have used “==” to searching a largest element in a dictionary. In this program we have to search max value of element that exists in a dictionary. So, output comes as “jaguar”.

import operator
car= {"audi": 100, "bmw": 1292, "jaguar": 210000, "hyundai":89}
x= max(car.items(), key = operator.itemgetter(1))[0]
print("the highest value is", x)

Output

the highest value is jaguar

Conclusion

In this article, we have discussed all methods to find the largest element in a dictionary briefly.

Updated on: 24-Apr-2023

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements