Saturday, October 25, 2025
HomeLanguagesPython – Dictionary with maximum count of pairs

Python – Dictionary with maximum count of pairs

Given dictionary list, extract dictionary with maximum keys.

Input : test_list = [{“gfg”: 2, “best” : 4}, {“gfg”: 2, “is” : 3, “best” : 4, “CS” : 9}, {“gfg”: 2}]
Output : 4
Explanation : 2nd dictionary has maximum keys, 4.

Input : test_list = [{“gfg”: 2, “best” : 4}, {“gfg”: 2}]
Output : 2
Explanation : 1st dictionary has maximum keys, 2.

Method #1 : Using len() + loop

In this, we iterate for each of dictionary and compare lengths of each, record and return one with maximum length.

Python3




# Python3 code to demonstrate working of 
# Dictionary with maximum keys
# Using loop + len()
  
# initializing list
test_list = [{"gfg": 2, "best" : 4}, 
             {"gfg": 2, "is" : 3, "best" : 4}, 
             {"gfg": 2}]
  
# printing original list
print("The original list is : " + str(test_list))
  
res = {} 
max_len = 0
for ele in test_list:
      
    # checking for lengths
    if len(ele) > max_len: 
        res = ele
        max_len = len(ele)
          
# printing results
print("Maximum keys Dictionary : " + str(res))


Output

The original list is : [{'gfg': 2, 'best': 4}, {'gfg': 2, 'is': 3, 'best': 4}, {'gfg': 2}]
Maximum keys Dictionary : {'gfg': 2, 'is': 3, 'best': 4}

Method #2 : Using max() + key=len

In this, we compute maximum length key using max() by passing additional key “len” for comparison based on lengths.

Python3




# Python3 code to demonstrate working of 
# Dictionary with maximum keys
# Using max() + key = len
  
# initializing list
test_list = [{"gfg": 2, "best" : 4}, 
             {"gfg": 2, "is" : 3, "best" : 4}, 
             {"gfg": 2}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# maximum length dict using len param
res = max(test_list, key = len)
          
# printing results
print("Maximum keys Dictionary : " + str(res))


Output

The original list is : [{'gfg': 2, 'best': 4}, {'gfg': 2, 'is': 3, 'best': 4}, {'gfg': 2}]
Maximum keys Dictionary : {'gfg': 2, 'is': 3, 'best': 4}
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS