-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter 19 - Dictionaries.py
35 lines (26 loc) · 1.1 KB
/
Chapter 19 - Dictionaries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# CHAPTER 19
# dictionary = a changeable, unordered collection of unique key:value pairs
# Fast because they use hashing, allow us to access value quickly
capitals = {
'USA': 'Washington DC',
'India': 'New Delhi',
'China': 'Beijing',
'Russia': 'Moscow'
}
capitals.update({'Germany': 'Berlin'}) # adds a new key:value pair
capitals.update({'USA': 'Las Vegas'}) # updates a new key:value pair
capitals.pop('China') # removes a key:value pair
capitals.clear() # clears all key:value pairs in the dictionary
# print(capitals['Russia'])
# a much safer way to get a value from a dictionary
# print(capitals.get('Germany'))
# print(capitals.keys()) # prints all the keys of capitals dictionary
# print(capitals.values()) # prints all the values of capitals dictionary
# One item is equal to one key:value pair
# print(capitals.items()) # prints all the items inside capitals dictionary
# prints all the items in your dictionary
for key,value in capitals.items():
print(key,value)
# this is an error since there is no
# "Germany" key in the capitals dictionary
# print(capitals['Germany'])