Dictionary in Python programming | Hands on Coding

Dictionary in Python
  • Dictionary is an unordered collection of data of different data types.
  • Items of dictionary can be changed.
  • A dictionary is created using curly braces.
  • Dictionary items are in the form of key-value pairs.
dict_name={key1:value1,key2:value2,key3:value3,.....}
student={
    "name":"Rocky Singh",
    "rollno":305,
    "percent":85.6
}
print(student)
"""
***Output***
{'name': 'Rocky Singh', 'rollno': 305, 'percent': 85.6}
"""
  • We can access items of dictionary using key name.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6
}
print("Student Name:",student["name"])
"""
***Output***
Student Name: Amisha
"""
  • We can also access items of dictionary using get() function.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6
}
print("Student Name:",student.get("name"))
"""
***Output***
Student Name: Amisha
"""
  • Printing key.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6
}
for key in student:
    print(key)
"""
***Output***
name
rollno
percent
"""
  • Printing key and value.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6
}
for key in student:
    print(key,"=",student[key])
"""
***Output***
name = Amisha
rollno = 305
percent = 85.6
"""
  • We can change the value of dictionary.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6
}
print("Dictionary before Update:\n",student)
#this line will replace 85.6 with 92.3
student["percent"]=92.3
print("Dictionary after Update:\n",student)
"""
***Output***
Dictionary before Update:
 {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Dictionary after Update:
 {'name': 'Amisha', 'rollno': 305, 'percent': 92.3}
"""
  • len() function is used to get length of dictionary.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
    "branch":"Computer Science"
}
print("Length of dictionary:",len(student))
"""
***Output***
Length of dictionary: 4
"""
  •  we can also add new item to a dictionary using ney key.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary:",student)
#adding new item to a dictionary
student["branch"]="Computer Science"
print("Dictionary:",student)
"""
***Output***
Dictionary: {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Dictionary: {'name': 'Amisha', 'rollno': 305, 'percent': 85.6, 'branch': 'Computer Science'}
"""
  • pop() function is used to remove specified item from a dictionary.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary before deletion:\n",student)
#Delete item from dictionary
student.pop("rollno")
print("Dictionary after deletion:\n",student)
"""
***Output***
Dictionary before deletion:
 {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Dictionary after deletion:
 {'name': 'Amisha', 'percent': 85.6}
 """
 
  • del keyword is also used to remove specified item from a dictionary.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary before deletion:\n",student)
#Delete item from dictionary
del student["rollno"]
print("Dictionary after deletion:\n",student)
"""
***Output***
Dictionary before deletion:
 {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Dictionary after deletion:
 {'name': 'Amisha', 'percent': 85.6}
 """
 
  • clear() function is used to clear or empty the dictionary.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary before clear:\n",student)
#this line will empty the dictionary
student.clear()
print("Dictionary after clear:\n",student)
"""
***Output***
Dictionary before clear:
 {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Dictionary after clear:
 {}
 """
 
  • del keyword is also used to delete dictionary completely.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary :\n",student)
#this line will empty the dictionary
del student
print("Deleted successfully")
"""
***Output***
Dictionary :
 {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Deleted successfully
 """