This note mainly focuses on summarizing knowledge based on Corey Schafer’s Python Tutorial.
Dictionary is a collection of key-value pairs.
Creating Dictionaries
We use curly braces notation to represent a dictionary.
empty_dict = {}
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']} print(student)
|
{'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']}
|
- Use keys to look up the values.
- Use colons (
:
) to separate the key and the value.
- Use commas (
,
) to separate key-value pairs.
Similar to string
, list
, etc., we can use len
to get the number of the dict
’s keys.
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']} print(len(student))
|
Accessing Dictionaries
Using Square Brackets to Access
We can square brackets ([<Key>]
) to access <Value>
.
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']} print(student['name'])
|
However, if the <Key>
does not exist, the KeyError
will occur.
For example, we want to get phone
of student
.
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']} print(student['phone'])
|
print(student['phone']) ~~~~~~~^^^^^^^^^ KeyError: 'phone'
|
Using get
Method to Access
To avoid KeyError
, we can use get
method.
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']} print(student.get('name'))
|
If the <Key>
does not exist, it will return default value None
.
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']} print(student.get('phone'))
|
We can also set default value for <Key>
not existing.
For example, if we cannot get
'phone'
, it return 'Not Found'
.
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']} print(student.get('phone', 'Not Found'))
|
Accessing All the Keys and Values
Method |
Description |
keys() |
All the keys in dict . |
values() |
All the values in the dict |
items() |
All the key-value pairs in the dict . |
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']}
print(student.keys()) print(student.values()) print(student.items())
|
dict_keys(['name', 'age', 'course']) dict_values(['John', 25, ['Math', 'CompSci']]) dict_items([('name', 'John'), ('age', 25), ('course', ['Math', 'CompSci'])])
|
Modifying Dictionaries
Update Entries in Dictionaries
Similar to accessing dictionaries, we can also use []
to:
- Add new entries
- Update values
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']}
student['phone'] = '555-5555' student['name'] = 'Jane'
print(student)
|
{'name': 'Jane', 'age': 25, 'course': ['Math', 'CompSci'], 'phone': '555-5555'}
|
We can use dict
’s method update
to achieve the same effect.
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']} student.update({'name': 'Jane', 'phone': '555-5555'}) print(student)
|
{'name': 'Jane', 'age': 25, 'course': ['Math', 'CompSci'], 'phone': '555-5555'}
|
Delete Entries in Dictionaries
There are to ways to delete entries:
- Using
del
function
- Using
pop
method
Firstly, we use del
function to remove age
from student
.
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']} del student['age'] print(student.get('age', 'Not Found'))
|
Similar to lists, we can use pop
to achieve entries’ removal.
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']} age = student.pop('age') print(age) print(student.get('age', 'Not Found'))
|
Traversing Dictionaries
We can use for-each loops to traverse the keys, values, key-value pairs in dictionaries.
student = {'name': 'John', 'age': 25, 'course': ['Math', 'CompSci']}
print('1. Traverse all the keys') for key in student: print(key)
print('\n' + '2. Traverse all the values') for value in student.values(): print(value)
print('\n' + '3. Traverse all the key-value pairs') for key, value in student.items(): print(key, value)
|
1. Traverse all the keys name age course
2. Traverse all the values John 25 ['Math', 'CompSci']
3. Traverse all the key-value pairs name John age 25 course ['Math', 'CompSci']
|