SlideShare une entreprise Scribd logo
1  sur  16
Télécharger pour lire hors ligne
Dictionaries
Week 8
Course: Programming in Python
CEC-Swayam/EMRC Dibrugarh University
Dictionaries
Dictionary is a built-in Python Data
Structure and are used to store data
values in key:value pairs. Each key is
separated from its value by a colon ( : ).
Dictionaries are not indexed by a
sequence of numbers but indexed
based on keys
Creating a Dictionary
• The syntax for defining a dictionary is:
• dictionary_name = {key_1: value_1, key_2: value2, key_3: value_3}
• Or it can also be written as :
• dictionary_name = { key_1: value_1,
key_2: value_2,
key_3: value_3,
}
Points to
remember
The keys in the dictionary must be unique and
of immutable data type i.e. strings, numbers or
tuples.
The value doesn't have any such restrictions.
Dictionary are case-sensitive i.e. two keys with
similar name but different case will be treated
differently.
The elements within the dictionary are
accessed with the help of the keys rather than
its relative position.
"""Write a program to create a dictionary to convert values from
meters to centimeters
"""
mtocm={m:m*100 for m in range(1,11) }
print("Meters:Centimeters",mtocm)
"""
Write a program that creates a dictionary of cubes of odd numbers in
the range (1-10)
"""
cubes={c:c**3 for c in range(10) if c%2==1}
print(cubes)
"""
To count the number of occurrences of each character of a message
entered by the user.
"""
def cnt(msg):
lc={} #empty dictionary
for l in msg:
lc[l]=lc.get(l,0)+1
print(lc)
msg=input("Enter a message ")
cnt(msg)
"""
Create a dictionary with names of studenst and marks in two papers.Create a dictionaryfinal which has names and total marks and also find the
topper.
"""
result={'Rahul':[78,89],
'Pranamika':[89,87],
'Ashish':[79,88],
'Anshul':[90,67]}
total=0
final=result.copy()
for key,val in result.items():
total=sum(val)
final[key]=total
print(final)
hig=0
Topper=''
for key,val in final.items():
if val>hig:
hig=val
Topper=key
print("Topper is :" , Topper, "securing ", hig, "marks")
"""
To get the minimum and maximum value from a dictionary
"""
dict = {
'Physics': 90,
'Chemistry':75,
'Maths': 85,
'English':87,
'Computer Sc.':96
}
print('Minimummarks in:', min(dict,key=dict.get))
print('Maximummarks in:', max(dict,key=dict.get))
"""
Change value of a key in a nested dictionary
"""
dict = {
'emp1': {'name': 'Akash', 'salary': 15500},
'emp2': {'name': 'Ajay', 'salary': 18000},
'emp3': {'name': 'Vijay', 'salary': 16500}
}
dict['emp2']['salary'] = 15500
print(dict)
# Program to print sum of key-value # pairs in dictionary
dict = {1: 34, 2: 29, 3: 49}
sumval = []
# Traverse the dictionary
for keys in dict:
sumval.append(keys + dict[keys])
# Print the list
print("Key-value sum =",sumval)
# Program for handling missing keys in the dictionary using get() method in Python
# Crating the dictionary
names = {'Sharma' : 'CEO' , 'Saikia' : 'Manager' , 'Ali' : 'Executive'}
# Getting user input for the key
search_key = input("Enter the key to be searched:=> ")
# Logic to handle missing keys in dictionary
print(names.get(search_key, "Search key not present"))
# Python program to compare two dictionaries using == operator
emp1 = {'eid': 101, 'ename': 'Rajib', 'eAge': 24}
emp2 = {'eid': 101, 'ename': 'Rajib', 'eAge': 24}
emp3 = {'eid': 102, 'ename': 'Kumar', 'eAge': 25}
if emp1 == emp2:
print("emp1and emp2 are same dictionaries")
else:
print("emp1and emp2 are not same dictionaries")
if emp2 == emp3:
print("emp2and emp3 are same dictionaries")
else:
print("emp2and emp3 are not same dictionaries")
# Program to remove a key from dictionary using del in Python
empage = {"Ravi" : 24, "Ashok" : 22, "Vijay" : 25 }
print("The dictionary is :", empage)
del_k = input("Enter the key to be deleted: ")
# Removing the key from dictionary
del empage[del_k]
# Printing the dictionary
print("The dictionary after deletion is : ")
print(empage)
# Python program to sort dictionary key and values list
# Creating a list with list as values
result = {'Raju' : [88, 45, 75], 'ram' : [98, 79, 68]}
print("Initially the dictionary is " + str(result))
# Sorting dictionary
sort_res = dict()
for key in sorted(result):
sort_res[key]= sorted(result[key])
# Printing sorted dictionary
print("Dictionary aftersort of key and list value : ")
print(str(sort_res))
Thank You

Contenu connexe

Similaire à "Python Dictionary: The Key to Efficient Data Storage, Manipulation, and Versatile Programming in Python - Unleash the Power of Key-Value Pairs for Dynamic Mapping and Streamlined Coding"

Ch 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptxCh 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptxKanchanaRSVVV
 
Farhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionariesFarhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionariesFarhana Shaikh
 
PE1 Module 4.ppt
PE1 Module 4.pptPE1 Module 4.ppt
PE1 Module 4.pptbalewayalew
 
An Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in PythonAn Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in Pythonyashar Aliabasi
 
Python lab basics
Python lab basicsPython lab basics
Python lab basicsAbi_Kasi
 
UNIT 1 - Revision of Basics - II.pptx
UNIT 1 - Revision of Basics - II.pptxUNIT 1 - Revision of Basics - II.pptx
UNIT 1 - Revision of Basics - II.pptxNishanSidhu2
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notesGOKULKANNANMMECLECTC
 
2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppttocidfh
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Twoamiable_indian
 
Dictionary part 1
Dictionary part 1Dictionary part 1
Dictionary part 1RishuKaul2
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxCatherineVania1
 

Similaire à "Python Dictionary: The Key to Efficient Data Storage, Manipulation, and Versatile Programming in Python - Unleash the Power of Key-Value Pairs for Dynamic Mapping and Streamlined Coding" (20)

Ch 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptxCh 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptx
 
Chapter 16 Dictionaries
Chapter 16 DictionariesChapter 16 Dictionaries
Chapter 16 Dictionaries
 
Farhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionariesFarhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionaries
 
PE1 Module 4.ppt
PE1 Module 4.pptPE1 Module 4.ppt
PE1 Module 4.ppt
 
Dictionary
DictionaryDictionary
Dictionary
 
Dictionaries in python
Dictionaries in pythonDictionaries in python
Dictionaries in python
 
An Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in PythonAn Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in Python
 
Python lab basics
Python lab basicsPython lab basics
Python lab basics
 
PYTHON.pdf
PYTHON.pdfPYTHON.pdf
PYTHON.pdf
 
UNIT 1 - Revision of Basics - II.pptx
UNIT 1 - Revision of Basics - II.pptxUNIT 1 - Revision of Basics - II.pptx
UNIT 1 - Revision of Basics - II.pptx
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt
 
Dictionaries and Sets
Dictionaries and SetsDictionaries and Sets
Dictionaries and Sets
 
Dictionaries.pptx
Dictionaries.pptxDictionaries.pptx
Dictionaries.pptx
 
Python-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdfPython-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdf
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Two
 
Python_IoT.pptx
Python_IoT.pptxPython_IoT.pptx
Python_IoT.pptx
 
Dictionary part 1
Dictionary part 1Dictionary part 1
Dictionary part 1
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptx
 

Dernier

SOC Analyst Guide For Beginners SOC analysts work as members of a managed sec...
SOC Analyst Guide For Beginners SOC analysts work as members of a managed sec...SOC Analyst Guide For Beginners SOC analysts work as members of a managed sec...
SOC Analyst Guide For Beginners SOC analysts work as members of a managed sec...Varun Mithran
 
一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书A
 
APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0
APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0
APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0APNIC
 
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样Fi
 
Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303Dewi Agency
 
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样AS
 
The Rise of Subscription-Based Digital Services.pdf
The Rise of Subscription-Based Digital Services.pdfThe Rise of Subscription-Based Digital Services.pdf
The Rise of Subscription-Based Digital Services.pdfe-Market Hub
 
Abortion Clinic in Kwa thema +27791653574 Kwa thema WhatsApp Abortion Clinic ...
Abortion Clinic in Kwa thema +27791653574 Kwa thema WhatsApp Abortion Clinic ...Abortion Clinic in Kwa thema +27791653574 Kwa thema WhatsApp Abortion Clinic ...
Abortion Clinic in Kwa thema +27791653574 Kwa thema WhatsApp Abortion Clinic ...mikehavy0
 
100^%)( POLOKWANE))(*((+27838792658))*))௹ )Abortion Pills for Sale in Sibasa,...
100^%)( POLOKWANE))(*((+27838792658))*))௹ )Abortion Pills for Sale in Sibasa,...100^%)( POLOKWANE))(*((+27838792658))*))௹ )Abortion Pills for Sale in Sibasa,...
100^%)( POLOKWANE))(*((+27838792658))*))௹ )Abortion Pills for Sale in Sibasa,...musaddumba454
 
Discovering OfficialUSA.com Your Go-To Resource.pdf
Discovering OfficialUSA.com Your Go-To Resource.pdfDiscovering OfficialUSA.com Your Go-To Resource.pdf
Discovering OfficialUSA.com Your Go-To Resource.pdfSadaf Khan
 
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样Fi
 
Thank You Luv I’ll Never Walk Alone Again T shirts
Thank You Luv I’ll Never Walk Alone Again T shirtsThank You Luv I’ll Never Walk Alone Again T shirts
Thank You Luv I’ll Never Walk Alone Again T shirtsrahman018755
 
Washington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers ShirtWashington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers Shirtrahman018755
 
I’ll See Y’All Motherfuckers In Game 7 Shirt
I’ll See Y’All Motherfuckers In Game 7 ShirtI’ll See Y’All Motherfuckers In Game 7 Shirt
I’ll See Y’All Motherfuckers In Game 7 Shirtrahman018755
 
一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理A
 
一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理AS
 
原版定制英国赫瑞瓦特大学毕业证原件一模一样
原版定制英国赫瑞瓦特大学毕业证原件一模一样原版定制英国赫瑞瓦特大学毕业证原件一模一样
原版定制英国赫瑞瓦特大学毕业证原件一模一样AS
 
一比一定制(Temasek毕业证书)新加坡淡马锡理工学院毕业证学位证书
一比一定制(Temasek毕业证书)新加坡淡马锡理工学院毕业证学位证书一比一定制(Temasek毕业证书)新加坡淡马锡理工学院毕业证学位证书
一比一定制(Temasek毕业证书)新加坡淡马锡理工学院毕业证学位证书B
 
Free on Wednesdays T Shirts Free on Wednesdays Sweatshirts
Free on Wednesdays T Shirts Free on Wednesdays SweatshirtsFree on Wednesdays T Shirts Free on Wednesdays Sweatshirts
Free on Wednesdays T Shirts Free on Wednesdays Sweatshirtsrahman018755
 

Dernier (20)

SOC Analyst Guide For Beginners SOC analysts work as members of a managed sec...
SOC Analyst Guide For Beginners SOC analysts work as members of a managed sec...SOC Analyst Guide For Beginners SOC analysts work as members of a managed sec...
SOC Analyst Guide For Beginners SOC analysts work as members of a managed sec...
 
一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书
 
APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0
APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0
APNIC Policy Roundup presented by Sunny Chendi at TWNOG 5.0
 
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
 
Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303
 
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样
 
The Rise of Subscription-Based Digital Services.pdf
The Rise of Subscription-Based Digital Services.pdfThe Rise of Subscription-Based Digital Services.pdf
The Rise of Subscription-Based Digital Services.pdf
 
Abortion Clinic in Kwa thema +27791653574 Kwa thema WhatsApp Abortion Clinic ...
Abortion Clinic in Kwa thema +27791653574 Kwa thema WhatsApp Abortion Clinic ...Abortion Clinic in Kwa thema +27791653574 Kwa thema WhatsApp Abortion Clinic ...
Abortion Clinic in Kwa thema +27791653574 Kwa thema WhatsApp Abortion Clinic ...
 
100^%)( POLOKWANE))(*((+27838792658))*))௹ )Abortion Pills for Sale in Sibasa,...
100^%)( POLOKWANE))(*((+27838792658))*))௹ )Abortion Pills for Sale in Sibasa,...100^%)( POLOKWANE))(*((+27838792658))*))௹ )Abortion Pills for Sale in Sibasa,...
100^%)( POLOKWANE))(*((+27838792658))*))௹ )Abortion Pills for Sale in Sibasa,...
 
Discovering OfficialUSA.com Your Go-To Resource.pdf
Discovering OfficialUSA.com Your Go-To Resource.pdfDiscovering OfficialUSA.com Your Go-To Resource.pdf
Discovering OfficialUSA.com Your Go-To Resource.pdf
 
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样
 
GOOGLE Io 2024 At takes center stage.pdf
GOOGLE Io 2024 At takes center stage.pdfGOOGLE Io 2024 At takes center stage.pdf
GOOGLE Io 2024 At takes center stage.pdf
 
Thank You Luv I’ll Never Walk Alone Again T shirts
Thank You Luv I’ll Never Walk Alone Again T shirtsThank You Luv I’ll Never Walk Alone Again T shirts
Thank You Luv I’ll Never Walk Alone Again T shirts
 
Washington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers ShirtWashington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers Shirt
 
I’ll See Y’All Motherfuckers In Game 7 Shirt
I’ll See Y’All Motherfuckers In Game 7 ShirtI’ll See Y’All Motherfuckers In Game 7 Shirt
I’ll See Y’All Motherfuckers In Game 7 Shirt
 
一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理
 
一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理
 
原版定制英国赫瑞瓦特大学毕业证原件一模一样
原版定制英国赫瑞瓦特大学毕业证原件一模一样原版定制英国赫瑞瓦特大学毕业证原件一模一样
原版定制英国赫瑞瓦特大学毕业证原件一模一样
 
一比一定制(Temasek毕业证书)新加坡淡马锡理工学院毕业证学位证书
一比一定制(Temasek毕业证书)新加坡淡马锡理工学院毕业证学位证书一比一定制(Temasek毕业证书)新加坡淡马锡理工学院毕业证学位证书
一比一定制(Temasek毕业证书)新加坡淡马锡理工学院毕业证学位证书
 
Free on Wednesdays T Shirts Free on Wednesdays Sweatshirts
Free on Wednesdays T Shirts Free on Wednesdays SweatshirtsFree on Wednesdays T Shirts Free on Wednesdays Sweatshirts
Free on Wednesdays T Shirts Free on Wednesdays Sweatshirts
 

"Python Dictionary: The Key to Efficient Data Storage, Manipulation, and Versatile Programming in Python - Unleash the Power of Key-Value Pairs for Dynamic Mapping and Streamlined Coding"

  • 1. Dictionaries Week 8 Course: Programming in Python CEC-Swayam/EMRC Dibrugarh University
  • 2. Dictionaries Dictionary is a built-in Python Data Structure and are used to store data values in key:value pairs. Each key is separated from its value by a colon ( : ). Dictionaries are not indexed by a sequence of numbers but indexed based on keys
  • 3. Creating a Dictionary • The syntax for defining a dictionary is: • dictionary_name = {key_1: value_1, key_2: value2, key_3: value_3} • Or it can also be written as : • dictionary_name = { key_1: value_1, key_2: value_2, key_3: value_3, }
  • 4. Points to remember The keys in the dictionary must be unique and of immutable data type i.e. strings, numbers or tuples. The value doesn't have any such restrictions. Dictionary are case-sensitive i.e. two keys with similar name but different case will be treated differently. The elements within the dictionary are accessed with the help of the keys rather than its relative position.
  • 5. """Write a program to create a dictionary to convert values from meters to centimeters """ mtocm={m:m*100 for m in range(1,11) } print("Meters:Centimeters",mtocm)
  • 6. """ Write a program that creates a dictionary of cubes of odd numbers in the range (1-10) """ cubes={c:c**3 for c in range(10) if c%2==1} print(cubes)
  • 7. """ To count the number of occurrences of each character of a message entered by the user. """ def cnt(msg): lc={} #empty dictionary for l in msg: lc[l]=lc.get(l,0)+1 print(lc) msg=input("Enter a message ") cnt(msg)
  • 8. """ Create a dictionary with names of studenst and marks in two papers.Create a dictionaryfinal which has names and total marks and also find the topper. """ result={'Rahul':[78,89], 'Pranamika':[89,87], 'Ashish':[79,88], 'Anshul':[90,67]} total=0 final=result.copy() for key,val in result.items(): total=sum(val) final[key]=total print(final) hig=0 Topper='' for key,val in final.items(): if val>hig: hig=val Topper=key print("Topper is :" , Topper, "securing ", hig, "marks")
  • 9. """ To get the minimum and maximum value from a dictionary """ dict = { 'Physics': 90, 'Chemistry':75, 'Maths': 85, 'English':87, 'Computer Sc.':96 } print('Minimummarks in:', min(dict,key=dict.get)) print('Maximummarks in:', max(dict,key=dict.get))
  • 10. """ Change value of a key in a nested dictionary """ dict = { 'emp1': {'name': 'Akash', 'salary': 15500}, 'emp2': {'name': 'Ajay', 'salary': 18000}, 'emp3': {'name': 'Vijay', 'salary': 16500} } dict['emp2']['salary'] = 15500 print(dict)
  • 11. # Program to print sum of key-value # pairs in dictionary dict = {1: 34, 2: 29, 3: 49} sumval = [] # Traverse the dictionary for keys in dict: sumval.append(keys + dict[keys]) # Print the list print("Key-value sum =",sumval)
  • 12. # Program for handling missing keys in the dictionary using get() method in Python # Crating the dictionary names = {'Sharma' : 'CEO' , 'Saikia' : 'Manager' , 'Ali' : 'Executive'} # Getting user input for the key search_key = input("Enter the key to be searched:=> ") # Logic to handle missing keys in dictionary print(names.get(search_key, "Search key not present"))
  • 13. # Python program to compare two dictionaries using == operator emp1 = {'eid': 101, 'ename': 'Rajib', 'eAge': 24} emp2 = {'eid': 101, 'ename': 'Rajib', 'eAge': 24} emp3 = {'eid': 102, 'ename': 'Kumar', 'eAge': 25} if emp1 == emp2: print("emp1and emp2 are same dictionaries") else: print("emp1and emp2 are not same dictionaries") if emp2 == emp3: print("emp2and emp3 are same dictionaries") else: print("emp2and emp3 are not same dictionaries")
  • 14. # Program to remove a key from dictionary using del in Python empage = {"Ravi" : 24, "Ashok" : 22, "Vijay" : 25 } print("The dictionary is :", empage) del_k = input("Enter the key to be deleted: ") # Removing the key from dictionary del empage[del_k] # Printing the dictionary print("The dictionary after deletion is : ") print(empage)
  • 15. # Python program to sort dictionary key and values list # Creating a list with list as values result = {'Raju' : [88, 45, 75], 'ram' : [98, 79, 68]} print("Initially the dictionary is " + str(result)) # Sorting dictionary sort_res = dict() for key in sorted(result): sort_res[key]= sorted(result[key]) # Printing sorted dictionary print("Dictionary aftersort of key and list value : ") print(str(sort_res))