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.

Find the product of all items in the dictionary.

In this python dictionary program, we will find the product of all items in the dictionary.

Steps to solve the program
  1. Take a dictionary as input and create a variable and assign its value equal to 1.
  2. Use for loop to iterate over values in the dictionary using values().
  3. During iteration multiply the value by the variable that we have created.
  4. Print the output.
				
					dict1 =  {'a':2,'b':4,'c':5}
result = 1

for val in dict1.values():
    result *= val
    
print(result)
				
			

Output :

				
					40
				
			

create a dictionary where keys are between 1 to 5 and values are squares of the keys.

remove a key from the dictionary.

Create a dictionary where keys are numbers and values are squares of the keys.

In this python dictionary program, we will create a dictionary where keys are between 1 to 5 and values are squares of the keys.

Steps to solve the program
  1. Create an empty dictionary.
  2. Use for loop with the range function to iterate over numbers from 1-5.
  3. Add these numbers as keys and their squares as the values.
  4. Use n**2 to get the square.
  5. Print the output.
				
					dict1 = {}

for i in range(1,6):
    dict1[i] = i**2
    
print(dict1)
				
			

Output :

				
					{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
				
			

insert a key at the beginning of the dictionary.

find the product of all items in the dictionary.

Insert a key at the beginning of the dictionary.

In this python dictionary program, we will insert a key at the beginning of the dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Create a new dictionary of the key which you want to update.
  3. Now concatenate the new dictionary with the old dictionary using update().
  4. So, the new key will be the first key.
  5. Print the output.
				
					dict1 = {'course':'python','institute':'sqatools' }
dict2 = {'name':'omkar'}

dict2.update(dict1)

print(dict2)
				
			

Output :

				
					{'name': 'omkar', 'course': 'python', 'institute': 'sqatools'}
				
			

create a dictionary in the form of (n^3)

create a dictionary where keys are between 1 to 5 and values are squares of the keys.

Python program to create a dictionary in the given form.

In this python dictionary program, we will create a dictionary in the given form of (n^3).

Steps to solve the program
  1. Take the value of n and create an empty dictionary.
  2. User for loop with range function to iterate over the given number.
  3. Add that number as the key and its cube as its value.
  4. For calculating the cube use n**3.
  5. Print the output.
				
					n = 4
D1 = {}

for i in range(1,4+1):
    D1.update({i:i**3})

print(D1)
				
			

Output :

				
					{1: 1, 2: 8, 3: 27, 4: 64}
				
			

iterate over a dictionary.

insert a key at the beginning of the dictionary.