Python Programming with Corey Schafer
Lecture 5 - Dictionaries
• Key value pairs ar two linked values where key is the unique identifier of data and the value is
the data
• Represent a student using a dictionary ( ‘:’ is used to separate key from the value)
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
print(student)
→ {'name': 'John', 'age': '25', 'courses': ['Maths', 'English']}
• Values in dictionary can be anything i.e. name is string, age is integer and courses is list
• To get value of one key
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
print(student['name']) / print(student['courses'])
→ John / ['Maths', 'English']
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
print(student['phone'])
→ error
As the key doesn’t exists
• To get value of one key using get method
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
print(student.get('name'))
→ John
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
print(student.get('phone'))
→ None
• To set a default value for a key that does not exists
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
print(student.get('phone', 'Not found'))
→ Not found
• Add a new entry to the dictionary or replace/update an entry
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
student ['phone'] = '555-5555'
print(student.get('phone', 'Not found'))
→ 555-5555
As it will add the key and the value of the key
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
student ['name'] = 'Jane'
print(student.get('name'))
→ Jane
As it will update the value of the key
• To update values using update method (useful for updating multiple values at a time)
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
student.update({'name' : 'Jane', 'age' : '28', 'phone' : '555-5555'})
print(student)
→ {'name': 'Jane', 'age': '28', 'courses': ['Maths', 'English'], 'phone': '555-5555'}
• Delete a key from the dictionary
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
del student ['age']
print(student)
→ {'name': 'John', 'courses': ['Maths', 'English']}
It will remove the key
• Delete a key from the dictionary using pop method (popped value can also be used)
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
age = student.pop ('age')
print(student)
print(age)
→ {'name': 'John', 'courses': ['Maths', 'English']}
25
• Check number of keys in the dictionary
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
print(len(student))
→3
• Check the keys in the dictionary
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
print(student.keys())
→ dict_keys(['name', 'age', 'courses'])
• Check the values in the dictionary
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
print(student.values())
→ dict_values(['John', '25', ['Maths', 'English']])
• Check the pairs (keys and values) in the dictionary
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
print(student.items())
→ dict_items([('name', 'John'), ('age', '25'), ('courses', ['Maths', 'English'])])
• To loop through keys
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
for key in student:
print(key)
→ name
age
courses
• To loop through keys and values
student = {"name" : "John", "age" : "25", "courses" : ["Maths", "English"]}
for key, value in student.items():
print(key, value)
→ name John
age 25
courses ['Maths', 'English']