Change language

Python program to find the 3 highest values ​​in a dictionary

| | |

Examples :

  Input:  my_dict = {’A’: 67,’ B’: 23, ’C’: 45,’ D’: 56, ’E’: 12,’ F’: 69}  Output:  {’F’: 69,’ A’: 67, ’D’: 56} 

Let’s see the different methods, we can find the 3 highest values ​​in the dictionary.

Method # 1: Using collection.Counter ()

Counter — it is a dict subclass for counting hash objects. It is an unordered collection where items are stored as dictionary keys and their counters are stored as dictionary values. Count can be any integer value, including zero or negative count. The Counter class is similar to bags or multisets in other languages.

most_common ([n]) returns a list of n most common items and their number from common to smallest.

# Python program for demonstration
# find the 3 highest values ​​in dictionary

 

from collections import Counter

  
# Initial vocabulary

my_dict = { ’A’ : 67 , ’B’ : 23 , ’C’ : 45

’D’ : 56 , ’E’ : 12 , ’ F’ : 69 }

  

k = Counter (my_dict)

 
# Find the 3 highest values ​​

high = k.mos t_common ( 3

 

print ( "Initial Dictionary:" )

print (my_dict, "" )

 

 

print ( "Dictionary with 3 highest values:" )

print ( "Keys: Values" )

 

for i in high:

  print (i [ 0 ], ":" , i [ 1 ], "" )

Exit:

 Initial Dictionary: {’C’ : 45, ’B’: 23,’ D’: 56, ’A’: 67,’ E’: 12, ’F’: 69} Dictionary with 3 highest values: Keys: Values ​​F: 69 A: 67 D: 56 

Method # 2: Using heapq.nlargest()

# Python program for demonstration
# find the 3 highest values ​​in the dictionary

from heapq import nlargest

  
# Initial dictionary

my_dict = { ’A’ : 67 , ’B’ : 23 , ’C’ : 45 ,

’D’ : 56 , ’E’ : 12 , ’ F’ : 69 }

  

print ( " Initial Dictionary: " )

print ( my_dict, "" )

 

ThreeHighest = nlargest ( 3 , my_dict, key = my_dict.get)

 

print ( "Dictionary with 3 highest values:" )

print ( "Keys: Values" )

  

for val in ThreeHighest:

print (val, ":" , my_dict.get (val))

Exit:

 Initial Dictionary: {’D’ : 56, ’E’: 12,’ F’: 69, ’C’: 45,’ B’: 23, ’A’: 67} Dictionary with 3 highest values: Keys: Values ​​F: 69 A: 67 D: 56 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically