Python program to remove duplicate values from Dictionary.

In this python dictionary program, we will remove duplicate values from Dictionary.

Steps to solve the program
  1. Take a dictionary as input and create an empty dictionary and list.
  2. Use for loop to iterate over keys and values of the dictionary using dictionary.items().
  3. If the value is not in the list then add that value and its key to the empty dictionary.
  4. Print the output.
				
					dict1 = {'a':12,'b':2,'c':12,'d':5,'e':35,'f':5}
dict2 = {}

for key,val in dict1.items():
    if val not in dict2.values():
        dict2[key] = val

print(dict2)
				
			

Output :

				
					{'a': 12, 'b': 2, 'd': 5, 'e': 35}
				
			

clear all items from the dictionary.

create a dictionary from the string.

Python program to clear all items from the dictionary.

In this python dictionary program, we will clear all items from the dictionary using an in-built function.

Steps to solve the program
  1. Take a dictionary as input.
  2. Clear all items from the given dictionary using clear().
  3. Print the output.
				
					dict1 = {'Name':'Harry','Rollno':345,'Address':'Jordan'}

dict1.clear()
print(dict1)
				
			

Output :

				
					{}
				
			

store squares of even and cubes of odd numbers in a dictionary using dictionary comprehension.

remove duplicate values from Dictionary.

Store squares of even and cubes of odd numbers in a dictionary using dictionary comprehension.

In this python dictionary program, we will store squares of even and cubes of odd numbers in a dictionary using the dictionary comprehension method.

Steps to solve the program
  1. Take a list as input and create an empty dictionary.
  2. Use for loop to iterate over list elements.
  3. If an element is an even number then store its square and if it is an odd number then store its cube in the dictionary.
  4. Use an if-else statement to check for odd and even numbers.
  5. Print the output.
				
					list1 = [4, 5, 6, 2, 1, 7, 11]
dict1 = {}

# iterate through the list of values
for val in list1:
    # val is divisible by 2, then value is even number
    if val % 2 == 0:
        # dict store value as square
        dict1[val] = val**2
    else:
        # dict store value as cube
        dict1[val] = val**3
    
print("result dictionary:", dict1)
				
			

 Output :

				
					result dictionary : {4: 16, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 1331}
				
			


Solution 2: solution with dictionary comprehension.
				
					input_list = [4, 5, 6, 2, 1, 7, 11]
dict_result={x:x**2 if x%2==0 else x**3 for x in input_list}
print("output dictionary :", dict_result)
				
			
 
Output:
				
					output dictionary : {4: 16, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 1331}
				
			

create a dictionary from two lists.

clear all items from the dictionary.

Python Program to create a dictionary from two lists.

In this python dictionary program, we will create a dictionary from two lists. Elements from the first list as keys and elements from the second as its values.

Steps to solve the program
  1. Take two lists as inputs and create an empty dictionary.
  2. Combined both lists by using the zip() function.
  3. Use for loop to iterate over the combined list.
  4. During iteration add elements from the first list as keys and elements from the second list as its values.
  5. Print the output.
				
					list1 = ['a','b','c','d','e']
list2 = [12,23,24,25,15,16]
dict1 = {}

for a,b in zip(list1,list2):
    dict1[a] = b
    
print(dict1)
				
			

Output :

				
					{'a': 12, 'b': 23, 'c': 24, 'd': 25, 'e': 15}
				
			

get a list of odd and even keys from the dictionary

store squares of even and cubes of odd numbers in a dictionary using dictionary comprehension.

Program to get a list of odd and even keys from the dictionary

In this python dictionary program, we will get a list of odd and even keys from the dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop to iterate over the keys in the dictionary.
  3. Use an if-else statement separate odd and even keys from the dictionary.
  4. Print the output.
				
					dict1 = {1:25,5:'abc',8:'pqr',21:'xyz',12:'def',2:'utv'}

list1 = [[val,dict1[val]] for val in dict1 if val%2 == 0]
list2 = [[val,dict1[val]] for val in dict1 if val%2 != 0]

print("Even key = ",list1)
print("Odd key = ",list2)
				
			

Output :

				
					Even key =  [[8, 'pqr'], [12, 'def'], [2, 'utv']]
Odd key =  [[1, 25], [5, 'abc'], [21, 'xyz']]
				
			

concatenate two dictionaries

create a dictionary from two lists.

Program to concatenate two dictionaries

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

Steps to solve the program
  1. Take two dictionaries as input.
  2. Concatenate two dictionaries to form a single dictionary using update().
  3. Print the output.
				
					dict1 = {'Name':'Harry','Rollno':345,'Address':'Jordan'}
dict2 = {'Age':25,'salary': '$25k'}

dict1.update(dict2)

print(dict1)
				
			

Output :

				
					{'Name': 'Harry', 'Rollno': 345, 'Address': 'Jordan', 'Age': 25, 'salary': '$25k'}
				
			

move items from dict1 to dict2.

get a list of odd and even keys from the dictionary

Move items from one dictionary to another.

In this python dictionary program, we will move items from one dictionary to another dictionary.

Steps to solve the program
  1. Take one dictionary as input and create another empty dictionary.
  2. Use for loop to iterate over input dictionary.
  3. Add values from the input dictionary to the empty dictionary.
  4. Print the output.
				
					dict1 = {'name':'john','city':'Landon','country':'UK'}
dict2 = {}
temp = dict1.copy()

for val in temp:
    v1 = dict1.pop(val)
    dict2[val] = v1

print("dictionary 2 :", dict2)
print("dictionary 1 :", dict1)

				
			

Output :

				
					dictionary 2 : {'name': 'john', 'city': 'Landon', 'country': 'UK'}
dictionary 1 : {}
				
			

print the square of all values in a dictionary.

concatenate two dictionaries

Print the square of all values in a dictionary.

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

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop to iterate over the dictionary.
  3. During iteration print the key and square of its value using **2.
  4. Print the output.
				
					dictionary = {'a': 5, 'b':3, 'c': 6, 'd': 8}

for val in dictionary:
    print(val,":", dictionary[val]**2)
				
			

Output :

				
					a : 25
b : 9
c : 36
d : 64
				
			

add elements to the dictionary.

move items from dict1 to dict2.

Python program to add elements to the dictionary. 

In this python dictionary program, we will add elements to the dictionary.

Steps to solve the program
  1. Create an empty dictionary by using {}.
  2. Add elements to the dictionary.
  3. To add elements use the following format: Dictionary_name[key_name] = value.
  4. Print the output.

Output :

				
					dictionary = {}
dictionary["Name"] = "Ketan"
dictionary["Age"] = "21"

print(dictionary)
				
			
				
					{'Name': 'Ketan', 'Age': '21'}
				
			

print the square of all values in a dictionary.

Program to perform mathematical operations on two numbers.

In this python basic program, we will perform mathematical operations on two numbers defined by the user.

Steps to solve the program
  1. Take two numbers and a mathematical operation of your choice (+,-,*,/) as input.
  2. Using if-else statements perform the corresponding operations on numbers.
  3. Print the output.
				
					num1 = float(input("Enter number 1: "))
num2 = float(input("Enter number 2: "))
operation = input("Enter operation of your choice: ")

if operation == "+":
    print(num1+num2)
elif operation == "-":
    print(num1-num2)
elif operation == "/":
    print(num1/num2)
elif operation == "*":
    print(num1*num2)
else:
    print("Invalid operation")
				
			

Output :

				
					Enter number 1: 25
Enter number 2: 5
Enter operation of your choice: /
5.0
				
			

calculate the volume of a sphere.