Show a dictionary with a maximum count of pairs.

In this python dictionary program, we will show a dictionary with a maximum count of pairs.

Steps to solve the program
  1. Take two dictionaries as input.
  2. Count the key-value pairs in both dictionaries using the len() function.
  3. Compare them by using ‘ > ‘ or ‘ ‘.
  4. Print the respective output.
				
					A = {'name':1,'age':2}
B = {'name':1,'age':2,'course':3,'institute':4}

count_a = len(A)
count_b = len(B)

if count_a > count_b:
    print("1st dicyionary has maximum pairs",count_a)
else:
    print("2nd dicyionary has maximum pairs",count_b)
				
			

Output :

				
					2nd dicyionary has maximum pairs 4
				
			

retrieve the value of the nested key indicated by the given selector list from a dictionary or list.

Extract Unique values from dictionary values.

Retrieve the value of the nested key indicated by the given selector list from a dictionary or list.

In this python dictionary program, we will retrieve the value of the nested key indicated by the given selector list from a dictionary or list.

Steps to solve the program
  1. From functools import reduce.
  2. From operator import getitem.
  3. Take a nested dictionary as input.
  4. Create a function to retrieve the value of the nested key indicated by the given selector list from a dictionary.
  5. Give 2 parameters to the function as input one is the dictionary and the second is the selectors.
  6. Using reduce and getitem retrieve the value of the nested key indicated by the given selector.
  7. Print the output.
				
					from functools import reduce 
from operator import getitem
D1 = {'p1':{'name':{'first':'lionel','last':'messi'},'team':['psg','argentina']}}

def retrive(d, selectors):
    return reduce(getitem, selectors, d)

print(retrive(D1, ['p1','name','last']))
print(retrive(D1, ['p1','team', 1]))
				
			

Output :

				
					messi
argentina
				
			

group the elements of a given list based on the given function.

show a dictionary with a maximum count of pairs.

Group the elements of a list based on the function.

In this python dictionary program, we will group the elements of a list based on the function.

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. Use for loop to iterate over the words in the dictionary.
  3. Add the word length as the key using the len() function and the word as its value to the empty dictionary.
  4. Print the output.
				
					l = ['abc','defg','hijkl']
D1 = {}

for val in l:
    D1[len(val)] = val
    
print(D1)
				
			

Output :

				
					{3: 'abc', 4: 'defg', 5: 'hijkl'}
				
			

get the total length of a dictionary with string values.

retrieve the value of the nested key indicated by the given selector list from a dictionary or list.

Python program to get the total length of a dictionary.

In this python dictionary program, we will get the total length of a dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use the len() function to calculate the length of the dictionary.
  3. The len() function will return the key-value pairs in the dictionary.
  4. To get the total number of items in the dictionary multiply the len() function by 2.
  5. Print the output.
				
					D1 = {'virat':50,'Rohit':40,'Rahul':25}

print(len(D1)*2)
				
			

Output :

				
					6
				
			

create key-value list pairings in a given dictionary.

group the elements of a given list based on the given function.

Create key-value list pairings in a given dictionary

In this python dictionary program, we will create key-value list pairings in a given dictionary

Steps to solve the program
  1. Take a dictionary in the given format as input.
  2. From itertools import product.
  3. Use for loop and pass the values to the product() function.
  4. Using the zip() function combine the key and its respective value as a single key-value pair.
  5. Convert these pairs into a dictionary using dict() and store the result in a variable.
  6. Print the dictionary inside a list.
				
					from itertools import product
D1 = {1:['Virat Kohli'],2:['Rohit Sharma'],3:['Hardik Pandya']}

for sub in product(*D1.values()):
    result = [dict(zip(D1, sub))]
    
print(result)
				
			

Output :

				
					[{1: 'Virat Kohli', 2: 'Rohit Sharma', 3: 'Hardik Pandya'}]
				
			

count the frequency in a given dictionary.

get the total length of a dictionary with string values.

Python program to count the frequency in a dictionary.

In this python dictionary program, we will count the frequency in a dictionary.

Steps to solve the program
  1. From collections import Counter.
  2. Take a dictionary as input.
  3. Pass the values in the dictionary to the Counter and store the result in a variable.
  4. Print the output.
				
					from collections import Counter
D1 = {'a':10,'b':20,'c':25,'d':10,'e':30,'f':20}    

result = Counter(D1.values())

print(result)
				
			

Output :

				
					Counter({10: 2, 20: 2, 25: 1, 30: 1})
				
			

find the shortest list of values with the keys in a given dictionary.

create key-value list pairings in a given dictionary.

Shortest list of values with the keys in a dictionary.

In this python dictionary program, we will find the shortest list of values with the keys in a given dictionary.

Steps to solve the program
  1. Take a dictionary in the given format as input and create an empty list.
  2. Use for loop to iterate over keys and values of the input dictionary.
  3. If the length of the value list is 1, then add the key for the value list to the empty list.
  4. Print the output.
				
					D1 = {'a':[10,12],'b':[10],'c':[10,20,30,40],'d':[20]}
keys = []

for k,v in D1.items():
    if len(v) == 1:
        keys.append(k)
        
print(keys)
				
			

Output :

				
					['b', 'd']
				
			

find the keys of maximum values in a given dictionary.

count the frequency in a given dictionary.

Find the keys of maximum values in a given dictionary.

In this python dictionary program, we will find the keys of maximum values in a given dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Sort the given dictionary in descending order by using sorted().
  3. In sorted use dictionary values as key to sort the dictionary using get().
  4. Print the keys of the first 2 maximum values in the dictionary using indexing.
				
					D1 =  {'a':18,'b':50,'c':36,'d':47,'e':60}
result = sorted(D1, key=D1.get, reverse=True)

print(result[:2])
				
			

Output :

				
					['e', 'b']
				
			

filter even numbers from a given dictionary value.

find the shortest list of values with the keys in a given dictionary.

Python program to filter even numbers from a dictionary value.

In this python dictionary program, we will filter even numbers from a dictionary value.

Steps to solve the program
  1. Take a dictionary in the given format as input.
  2. Create a function to filter even numbers from a dictionary value.
  3. Use for loop to iterate over keys and values of the dictionary.
  4. Use another for loop to iterate over elements in the value list.
  5. Add keys and value list and such the elements in the value list are even numbers to the other dictionary using an if statement.
  6. Return the dictionary.
  7. Now pass the input dictionary to the function to print the output.
				
					D1 = {'a':[11,4,6,15],'b':[3,8,12],'c':[5,3,10]}

def even(dictt):
    result = {key: [idx for idx in val if not idx % 2]  
          for key, val in dictt.items()}   
    return result

print(even(D1))
				
			

Output :

				
					{'a': [4, 6], 'b': [8, 12], 'c': [10]}
				
			

access the dictionary key.

find the keys of maximum values in a given dictionary.

Python program to access the dictionary key.

In this python dictionary program, we will access the dictionary key.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop with keys() function to access the keys of the dictionary.
  3. Print the output.
				
					D1 = {"pepsi":50,"sprite":60,"slice":55}

for key in D1.keys():
    print(key)
				
			

Output :

				
					pepsi
sprite
slice
				
			

remove keys with substring values.

filter even numbers from a given dictionary value.