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.

Update the list values in the dictionary.

In this python dictionnary program, we will update the list values in the dictionary.

Steps to solve the program
  1. Take a dictionary in the given format as input.
  2. Create a function to update the values.
  3. Use for loop to update values for each key of the dictionary.
  4. Add 10 to the values for the 1st key and subtract 15 from the values of the 2nd key.
  5. Return the dictionary.
  6. Now pass the input dictionary to the function to print the output.
				
					def add(dictionary):
    dictionary['virat'] = [x+10 for x in dictionary['virat']]
    dictionary['rohit'] = [x-25 for x in dictionary['rohit']]
    return dictionary

D1 = {'virat':[50,30],'rohit':[40,10]} 

print(add(D1))
				
			

Output :

				
					{'virat': [60, 40], 'rohit': [15, -15]}
				
			

clear the list values in the said dictionary.

extract a list of values from a given list of dictionaries

Python program to clear the list values in the dictionary.

In this python dictionary program, we will clear the list values in the dictionary.

Steps to solve the program
  1. Take a dictionary in the given format as input
  2. Use for loop to iterate over values of the dictionary.
  3. Clear the list of values using clear().
  4. Print the output.
				
					D1 = {'virat':[50,30],'rohit':[40,10]} 
for key in D1:
    D1[key].clear()

print(D1)
				
			

Output :

				
					{'virat': [], 'rohit': []}
				
			

convert string values of a given dictionary, into integer/float datatypes.

update the list values in the said dictionary.

Convert string values of a dictionary into integer/float datatypes.

In this python dictionary program, we will convert string values of a dictionary into integer/float datatypes.

Steps to solve the program
  1. Take two dictionaries as input and create two empty dictionaries.
  2. First, use for loop to iterate over keys and values of the 1st dictionaries.
  3. Add the keys and values to the 1st empty dictionary.
  4. While adding them convert the datatype of the value from a string to an integer datatype.
  5. Now repeat the above step for converting the datatype of the values from the 2nd dictionary from string to float.
  6. Add them to the 2nd empty dictionary.
  7. Print the output.
				
					A = { 'a': '30',  'b': '20', 'c': '10' }
D1 = {}
for k,v in A.items():
    D1[k] = int(v)

B =  { 'a': '3.33', 'b': '20.50', 'c': '12.5' } 
D2 = {}
for key,val in B.items():
    D2[key] = float(val)
    
print(D1)
print(D2)
				
			

Output :

				
					{'a': 30, 'b': 20, 'c': 10}
{'a': 3.33, 'b': 20.5, 'c': 12.5}

				
			

remove a specified dictionary from a given list.

clear the list values in the said dictionary.