Python program to rename key of a dictionary

In this python dictionary program, we will rename key of a dictionary

Steps to solve the program
  1. Take a dictionary as input.
  2. Add a new key and assign its value to the previous key using pop().
  3. In this way, we will rename the key for that value.
  4. Print the output.
				
					D1 = {'a':19,'b': 20,'c':21,'d':20}
D1['e'] = D1.pop('d')
        
print(D1)
				
			

Output :

				
					{'a': 19, 'b': 20, 'c': 21, 'e': 20}
				
			

delete a list of keys from a dictionary.

Invert a given dictionary with non-unique hashable values.

Python program to delete a list of keys from a dictionary.

In this python dictionary program, we will delete a list of keys from a dictionary.

Steps to solve the program
  1. Take a dictionary and a list of keys as input.
  2. Use for loop to iterate over keys in the list.
  3. Delete the keys from the diction using del.
  4. Print the output.
				
					D1 = {'a':19,'b': 20,'c':21,'d':20,'e':50}
Key = ['a','d','e']

for key in Key:
    del D1[key]
    
print(D1)
				
			

Output :

				
					{'b': 20, 'c': 21}
				
			

initialize a dictionary with default values.

rename key of a dictionary

Python program to initialize a dictionary with default values

In this python dictionary program, we will Initialize a dictionary with default values

Steps to solve the program
  1. Take a list of keys as input and create a dictionary to set the default values for those keys.
  2. Create an empty list.
  3. Use for loop to iterate over keys in the list and add those keys to the empty dictionary and set up their values as default values using another dictionary that we have created.
				
					Name = ['Virat','Rohit']
Defaults = {'sport':'cricket','salary':100000}
D1 = {}

for name in Name:
    D1[name] = Defaults
    
print(D1)
				
			

Output :

				
					{'Virat': {'sport': 'cricket', 'salary': 100000}, 'Rohit': {'sport': 'cricket', 'salary': 100000}}
				
			

create a flat list of all the values in a flat dictionary. 

delete a list of keys from a dictionary.

Create a list of all the values in a dictionary. 

In this python dictionary, we will create a flat list of all the values in a flat dictionary. 

Steps to solve the program
  1. Take a dictionary as input and create an empty list.
  2. Use for loop to iterate over values of the dictionary.
  3. Add the values to the list using the append() function.
  4. Print the output.
				
					D1 = {'sqa':1,'tools':2,'is':3,'best':4} 
l = []

for k in D1.values():
    l.append(k)
    
print(l)
				
			

Output :

				
					[1, 2, 3, 4]
				
			

create a flat list of all the keys in a flat dictionary.

initialize a dictionary with default values

Create a list of all the keys in a dictionary.

In this python dictionary program, we will create a flat list of all the keys in a flat dictionary.

Steps to solve the program
  1. Take a dictionary as input and create an empty list.
  2. Use for loop to iterate over keys of the dictionary.
  3. Add the keys to the list using the append() function.
  4. Print the output.
				
					D1 = {'sqa':[1,2,5,6],'tools':[3,8,9],'is':[2,5,0],'best':[3,6,8]} 
l = []

for k in D1.keys():
    l.append(k)
    
print(l)
				
			

Output :

				
					['sqa', 'tools', 'is', 'best']
				
			

convert given a dictionary to a list of tuples.

create a flat list of all the values in a flat dictionary. 

Convert given a dictionary to a list of tuples.

In this python dictionary program, we will convert given a dictionary to a list of tuples.

Steps to solve the program
  1. Take a dictionary as input.
  2. Convert the dictionary to a list of tuples using list() and items().
  3. Print the output.
				
					D1 = {'a':19,'b':20,'c':21,'d':20}
l = list(D1.items())
    
print(l)
				
			

Output :

				
					[('a', 19), ('b', 20), ('c', 21), ('d', 20)]
				
			

find all keys in the provided dictionary that have the given value.

create a flat list of all the keys in a flat dictionary.

Find all keys in the provided dictionary that have the given value.

In this python dictionary program, we will find all keys in the provided dictionary that have the given value.

Steps to solve the program
  1. Take a dictionary as input and create an empty list.
  2. Use for loop to iterate over keys and values of the dictionary.
  3. If a value is equal to 0 then add its key to the list using append().
  4. Print the output.
				
					D1 = {'a':19,'b':20,'c':21,'d':20}
l = []

for k,v in D1.items():
    if v == 20:
        l.append(k)
        
print(l)
				
			

Output :

				
					['b', 'd']
				
			

convert a list of dictionaries into a list of values corresponding to the specified key.

convert given a dictionary to a list of tuples.

Convert a list of dictionaries into a list of values.

In this python dictionary program, we will convert a list of dictionaries into a list of values corresponding to the specified key.

Steps to solve the program
  1. Take a list of dictionaries as input and create an empty list.
  2. Use for loop to iterate over dictionaries in the list.
  3. Get the value for the specified key in the dictionary using the get() function and add that value to the empty list.
  4. Print the output.
				
					l1 =  [{'name':'josh','age':30},{'name':'david','age':25},{'name':'virat','age':32}]
result = []

for x in l1:
    result.append(x.get('age'))

print(result)
				
			

Output :

				
					[30, 25, 32]

				
			

show keys associated with values in dictionary

find all keys in the provided dictionary that have the given value.

Show keys associated with values in dictionary.

In this python dictionary program, we will show keys associated with values in dictionary.

Steps to solve the program
  1. Take a dictionary as input and create an empty dictionary.
  2. Use for loop to iterate over key-value pairs in the input dictionary.
  3. Use a nested for loop to iterate over elements in the values list.
  4. Add the element as the key and the key for that value list as its value to the empty dictionary.
  5. Print the output.
				
					D1 = {'xyz':[20,40],'abc':[10,30]}
D2 = {}

for k,v in D1.items():
    for ele in v:
        D2[ele] = [k]
        
print(D2)
				
			

Output :

				
					{20: ['xyz'], 40: ['xyz'], 10: ['abc'], 30: ['abc']}

				
			

Extract Unique values from dictionary values.

convert a list of dictionaries into a list of values corresponding to the specified key.

Python program to extract unique values from dictionary values.

In this python dictionary program, we will extract unique values from dictionary values.

Steps to solve the program
  1. Take a dictionary in the given format as input and create an empty dictionary.
  2. Use for loop to iterate over the values in the dictionaries.
  3. Use a nested for loop to iterate the value list.
  4. If the element in the value list is not in the empty list then add that element to it using the append() function.
  5. Print the output.
				
					D1= {'sqa':[1,2,5,6],'tools':[3,8,9],'is':[2,5,0],'best':[3,6,8]}
value = []

for val in D1.values():
    for ele in val:
        if ele not in value:
            value.append(ele)
            
print(value)
				
			
				
					[1, 2, 5, 6, 3, 8, 9, 0]
				
			

show a dictionary with a maximum count of pairs.

show keys associated with values in dictionary