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.

Python program to remove keys with substring values.

In this python dictionary program, we will remove keys with substring values from a dictionary.

Steps to solve the program
  1. Take a dictionary and a list of substrings as input and create an empty dictionary.
  2. Use for loop to iterate over keys and values of the dictionary.
  3. Using an if statement and a for loop inside the any() function check whether the substring from the list exists in the list values or not.
  4. Add only those kay-value pairs in which the substring does not exist to the empty dictionary.
  5. Print the output.
				
					D1 = {1:'sqatools is best',2:'for learning python'}
substr = ['best','excellent']
res = dict()

for key, val in D1.items():
    if not any(ele in val for ele in substr):
        res[key] = val
            
print(res)
				
			

Output :

				
					{2: 'for learning python'}
				
			

remove keys with values greater than n.

access the dictionary key.

Program to remove keys with values greater than n.

In this python dictionary program, we will remove keys with values greater than n.

Steps to solve the program
  1. Take a dictionary and n as input and create an empty dictionary.
  2. Use for loop to iterate over keys and values of the input dictionary.
  3. If the value is less than 6 then add such key-value pairs to the empty dictionary.
  4. Print the output.
				
					A =  {'sqa':3,'tools':5,'python':7}
B = {}

for k,v in A.items():
    if v < 6:
        B[k] = v
        
print(B)
				
			

Output :

				
					{'sqa': 3, 'tools': 5}
				
			

extract key’s value if key is present in list and dictionary

remove keys with substring values.

Extract the key’s value if the key is present in the list and dictionary

In this python dictionary program, we will extract the key’s value if the key is present in the list and dictionary.

Steps to solve the program
  1. Take a dictionary and a key having a common value between them.
  2. Use for loop to iterate over keys and values of the dictionary.
  3. If the key is present in the dictionary, then print the value of that key.
				
					A = ['sqatools','is','best']
B = {'sqatools':10}

for k,v in B.items():
    if k in A:
        print(v)
				
			

Output :

				
					10
				
			

create nested Dictionary using List.

remove keys with values greater than n.

Program to create nested Dictionary using a list

In this python dictionary program, we will create a nested dictionary using a list.

Steps to solve the program
  1. Take a list and a dictionary as input and create an empty dictionary.
  2. Use for loop to iterate over a combination of list and dictionary using zip().
  3. The zip () function will combine the first element of the list with the first key-value pair of the dictionary.
  4. Add the list element as the key and the key-value pair from the dictionary as its value to the empty dictionary.
  5. Print the output.
				
					D1 = {'sqatools':8,'python':6}
l = [1,2]

res = {}
for key, ele in zip(l, D1.items()):
    res[key] = dict([ele])
    
print(res)
				
			

Output :

				
					{1: {'sqatools': 8}, 2: {'python': 6}}
				
			

get the depth of the dictionary.

extract key’s value if key is present in list and dictionary