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.

Python program to iterate over a dictionary.

In this python dictionary program, we will iterate over a dictionary using a for loop.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop to iterate over keys of the dictionary.
  3. With the help of the keys access the values of the respective keys.
  4. Print the output.
				
					D1 = {'food':'burger','type':'fast food'}

for val in D1:
    print(val,D1[val])
				
			

Output :

				
					food burger
type fast food
				
			

check whether a key exists in the dictionary or not.

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

Check whether a key exists in the dictionary or not.

In this python dictionary program, we will check whether a key exists in the dictionary or not.

Steps to solve the program
  1. Take a dictionary as input.
  2. Using an if-else statement check whether the given keys exist in the dictionary.
  3. If yes then the print key exists in the dictionary.
  4. If False then the print key does not exist.
				
					D1 = {'city':'pune','state':'maharashtra'}
count = 0

for key in D1.keys():
    if key == "Country":
        count += 1

if count > 0:
    print("Key exists")
else:
    print("Key does not exists")
				
			

Output :

				
					Key does not exists
				
			

get the size of a dictionary in python.

iterate over a dictionary.

Python program to get the size of a dictionary.

In this python dictionary program, we will get the size of a dictionary in python using sys library.

Steps to solve the program
  1. Import the sys library.
  2. Take a dictionary as input.
  3. Get the size of a dictionary using sys.getsizeof().
  4. Print the output.
				
					import sys
D1 = {'name':'virat','sport':'cricket'}

print("Size of dic1: " + str(sys.getsizeof(D1)) + "bytes")
				
			

Output :

				
					Size of dic1: 232bytes
				
			

get the sum of all the items in a dictionary.

check whether a key exists in the dictionary or not.

Get the sum of all the items in a dictionary.

In this python dictionary program, we will get the sum of all the items in a dictionary.

Steps to solve the program
  1. Take a dictionary as input and create a variable named total and assign its value equal to 0.
  2. Use for loop to iterate over all the values in the dictionary using values().
  3. Print the output. 
				
					D1 =  {'x':23,'y':10,'z':7}
total = 0
for val in D1.values():
    total += val
    
print(total)
				
			

Output :

				
					40
				
			

Swap the values of the keys in the dictionary.

get the size of a dictionary in python.

Program to swap the values of the keys in the dictionary.

In this python dictionary program, we will swap the values of the keys in the dictionary.

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 given dictionary using items().
  3. Swap the keys and values while adding to the other dictionary
  4. Print the output.
				
					D1 = {'name':'yash','city':'pune'}
D2 ={}

for key,value in D1.items():
    D2[value] = key
    
print(D2)
				
			

Output :

				
					{'yash': 'name', 'pune': 'city'}
				
			

concatenate two dictionaries

get the sum of all the items in a dictionary.

Python program to concatenate two dictionaries to from a single dictionary

In this python dictionary program, we will concatenate two dictionaries to form a single dictionary.

Steps to solve the program
  1. Take two dictionaries as input.
  2. Concatenate two dictionaries using update().
  3. Print the output.
				
					dict1 = {'name':'yash','city':'pune'}
dict2 =  {'course':'python','institute':'sqatools'}

dict1.update(dict2)

print(dict1)
				
			

Output :

				
					{'name': 'yash', 'city': 'pune', 'course': 'python', 'institute': 'sqatools'}
				
			

add a key in a dictionary.

swap the values of the keys in the dictionary.