๐Ÿ
B.TECH I YEAR โ€ข II SEMESTER โ€ข R25

Python Programming - Lab Manual

Python Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š II Semester ๐Ÿ Python Programming ๐Ÿงช Lab Program
Selected Program

read dictionary values

โ† All Python Programs

Aim:

☛   Write a python code to read dictionary values from the user. Construct a function to invert its content. i.e., keys should be values and values should be keys.

Solution :


PROGRAM: (invert_dictionary.py)

 
import ast
def swap_dict(old_dict):
	return { value : key for key , value in old_dict.items () }
old_dict = input(" Please enter a dictionary : \n ")
old_dict = ast.literal_eval(old_dict)
# Printing original dictionary
print(" Original dictionary is : \n " , old_dict)
print()
new_dict = swap_dict(old_dict)
# Printing new dictionary
print(" New dictionary is : \n " , new_dict)

OUTPUT:

 
 Please enter a dictionary :
 { 'A' : 65 , 'B' : 66 , 'C' : 67 , 'D' : 68 , 'E' : 69 , 'F' : 70 , 'G' : 71 , 'H' : 72 }
 Original dictionary is :
  {'A': 65, 'B': 66, 'C': 67, 'D': 68, 'E': 69, 'F': 70, 'G': 71, 'H': 72}

 New dictionary is :
  {65: 'A', 66: 'B', 67: 'C', 68: 'D', 69: 'E', 70: 'F', 71: 'G', 72: 'H'}



๐Ÿ’ป Run This Python Program

read dictionary values
The selected Python program is loaded automatically into the editor. Edit the program, provide input if required and click Run.

The compiler is embedded using OneCompiler. Your Python program runs inside the embedded Python environment; Study Glance does not execute Python code directly on your web server.
โ† Back to Python Lab Programs ๐Ÿ“š All Lab Manuals