Python program to print all the unique values in a dictionary.

In this python dictionary program, we will print all the unique values in a dictionary.

Steps to solve the program
  1. Take a list of dictionaries as input and create an empty list.
  2. Use a for loop to iterate over each dictionary in the list.
  3. Use a nested for loop to iterate over each dictionary value.
  4. If the value is not in the list that we have created, then add that value to the list using append().
  5. Print the output.
				
					list1 = [{'name1':'robert'},{'name2':'john'},{'name3':'jim'},
         {'name4':'robert'}]
list2 = []
for val in list1:
    for ele in val.values():
        if ele not in list2:
            list2.append(ele)
            
print(list2)
				
			

Output :

				
					['robert', 'john', 'jim']
				
			

add two dictionaries if the keys are the same then add their value.

display different combinations of letters from dictionary values.

Add two dictionaries if the keys are the same then add their value.

In this python dictionary program, we will add two dictionaries if the keys are the same then add their value.

Steps to solve the program
  1. Take two dictionaries as input.
  2. Using a nested for loop and an if-else statement add values of the common keys between the two dictionaries.
  3. Print the output.
				
					Dict1 = {'x':10,'y':20,'c':50,'f':44 }
Dict2 = {'x':60,'c':25,'y':56}

for key in Dict1:
    for k in Dict2:
        if key == k:
            a = Dict1[key]+Dict2[k]
            Dict2[key] = a
        else:
            pass
            
Dict2
				
			

Output :

				
					{'x': 70, 'c': 75, 'y': 76}
				
			

check whether a dictionary is empty or not.

print all the unique values in a dictionary.

Check whether a dictionary is empty or not.

In this python dictionary program, we will check whether a dictionary is empty or not.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use an if-else statement with the len() function to check whether a dictionary is empty or not.
  3. Use logic to print the output.
				
					dict1 = {}
    
if len(dict1) > 0:
    print("Dictionary is not empty")
else:
    print("Dictionary is empty")
				
			

Output :

				
					Dictionary is empty
				
			

remove duplicate values from dictionary values.

add two dictionaries if the keys are the same then add their value.

Program to remove duplicate values from dictionary values.

In this python dictionary program, we will remove duplicate values from 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 keys and values of the dictionary.
  3. Use a nested for loop to iterate over the value list.
  4. Using an if statement check for duplicate values. 
  5. If any duplicate value exists then remove it from the value list using remove().
  6. Add the key and the value list to the empty dictionary.
  7. Print the output.
				
					dict1 = {'marks1':[23,28,23,69],'marks2':[ 25, 14, 25]}

for key,val in dict1.items():
    for ele in val:
        if ele in val:
            val.remove(ele)

print(dict1)
				
			

Output :

				
					{'marks1': [28, 69], 'marks2': [14, 56]}
				
			

remove a word from the string if it is a key in a dictionary.

check whether a dictionary is empty or not.

Remove a word from the string if it is used as a key in a dictionary.

In this python dictionary program, we will remove a word from the string if it is a key in a dictionary.

Steps to solve the program
  1. Take a string and a dictionary as input.
  2. Create an empty string.
  3. Split the input string using split().
  4. Use for loop to iterate over keys of the dictionary.
  5. If the word from the split string is not used as a key in the dictionary, then add that word to the empty dictionary.
  6. Print the output.
				
					String = 'sqatools is best for learning python'
Dict = {'best':2,'learning':6}

str2 = ""
for word in String.split(" "):
    if word not in Dict:
        str2 += word + " "
        
print(str2)
				
			

Output :

				
					sqatools is for python 
				
			

replace words in a string using a dictionary.

remove duplicate values from dictionary values.

Program to replace words in a string using a dictionary.

In this python dictionary program, we will replace words in a string using a dictionary.

Steps to solve the program
  1. Take a string and a dictionary as input.
  2. Use for loop to iterate over the keys and values of the dictionary using items().
  3. If the key in the dictionary is used in the given string, then replace the key with its values in the dictionary using replace().
  4. Print the output.
				
					string = 'learning python at sqa-tools'
Dict = {'at':'is','sqa-tools':'fun'}

for key, value in Dict.items():
    string = string.replace(key, value)

print(string)
				
			

Output :

				
					learning python is fun
				
			

group the same items into a dictionary values.

remove a word from the string if it is a key in a dictionary.

Group the same items into a dictionary values list.

In this python dictionary program, we will group the same items into a dictionary values.

Stpes to solve the program
  1. Take a list of the same items as input.
  2. From collections import defaultdict.
  3. Create a variable and assign its value equal to defaultdict(list).
  4. Use for loop to iterate over items in the list.
  5. Add the item as a key and group the same items from the list as its value to the variable using append().
  6. Print the output.
				
					from collections import defaultdict
 
list1 = [1,3,4,4,2,5,3,1,5,5,2] 
print("The original list : ",list1)
 
dict1 = defaultdict(list)
for val in list1:
    dict1[val].append(val)
print("Similar grouped dictionary :" ,dict1)
				
			

Output :

				
					The original list :  [1, 3, 4, 4, 2, 5, 3, 1, 5, 5, 2]
Similar grouped dictionary : defaultdict(<class 'list'>, {1: [1, 1], 3: [3, 3], 4: [4, 4], 2: [2, 2], 5: [5, 5, 5]})
				
			

find maximum and minimum values in a dictionary.

replace words in a string using a dictionary.

Find maximum and minimum values in a dictionary.

In this python dictonary program, we will find maximum and minimum values in a 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 in the dictionary using values().
  3. Add the values to the empty list.
  4. Sort the list by using sort().
  5. Find the maximum and minimum values by using logic.
  6. Print the output.
				
					dict1 = {'a':10,'b':44,'c':60,'d':25}
list1 = []

for val in dict1.values():
    list1.append(val)
    
list1.sort()

print("Minimum value: ",list1[0])
print("Maximum value: ",list1[-1])
				
			

Output :

				
					Minimum value:  10
Maximum value:  60
				
			

map two lists into a dictionary.

group the same items into a dictionary values.

Python program to map two lists into a dictionary.

In this python dictionary program, we will map two lists into a dictionary.

Steps to solve the program
  1. Take two lists as input.
  2. Map two lists into a single dictionary using zip() and dict().
  3. Print the output.
				
					list1 =  ['name','sport','rank','age']
list2 =  ['Virat','cricket',1,32]

new_dict = dict(zip(list1,list2))

print(new_dict)
				
			

Output :

				
					{'name': 'Virat', 'sport': 'cricket', 'rank': 1, 'age': 32}
				
			

remove a key from the dictionary.

find maximum and minimum values in a dictionary.

Python program to remove a key from the dictionary.

In this python dictionary program, we will remove a key from the dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Using an if-else statement remove the given key from the dictionary.
  3. Print the output.
				
					dict1 =  {'a':2,'b':4,'c':5}
dict2={}

for key,val in dict1.items():
    if key != "c":
        dict2[key]=val
        
print(dict2)
				
			

Output :

				
					{'a': 2, 'b': 4}
				
			

find the product of all items in the dictionary.

map two lists into a dictionary.