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

Python program to find the length of dictionary values.

In this python dictionary program, we will find the length of dictionary values.

Steps to solve the program
  1. Take a dictionary as input and create an empty dictionary.
  2. Use for loop to iterate over the input dictionary.
  3. Add the word as key and its length as value to the empty dictionary using the len() function.
  4. Print the output.
				
					D1 = {1:'sql',2:'tools',3:'python'}
D2 = {}

for val in D1.values():
    D2[val] = len(val)
    
print(D2)
				
			

Output :

				
					{'sql': 3, 'tools': 5, 'python': 6}
				
			

extract a list of values from a given list of dictionaries

get the depth of the dictionary.

Extract a list of values from a given list of dictionaries

In this python dictionary program, we will extract a list of values from a given list of dictionaries

Steps to solve the program
  1. Take a list of dictionaries as input and create an empty list.
  2. First, iterate over dictionaries in the list using a for loop.
  3. Now to iterate over the keys and values of each dictionary use another for loop.
  4. Add the values to the empty list for the specified key.
  5. Print the output.
				
					D1 = [{'t20':50,'odi':70},{'t20':40,'odi':10},{'t20':30,'odi':0},{'t20':45,'odi':65}]
l = []

for ele in D1:
    for key,val in ele.items():
        if key == 'odi':
            l.append(val)
            
print(l)
				
			

Output :

				
					[70, 10, 0, 65]
				
			

update the list values in the said dictionary.

find the length of dictionary values.