-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathdictionariception.py
More file actions
40 lines (31 loc) · 1.46 KB
/
dictionariception.py
File metadata and controls
40 lines (31 loc) · 1.46 KB
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
36
37
38
39
40
'''
Dictionariception
100xp
Remember lists? They could contain anything, even other lists. Well, for dictionaries the
same holds. Dictionaries can contain key:value pairs where the values are again dictionaries.
As an example, have a look at the script where another version of europe - the dictionary you've
been working with all along - is coded. The keys are still the country names, but the values
are dictionaries that contain more information than just the capital.
It's perfectly possible to chain square brackets to select elements. To fetch the population
for Spain from europe, for example, you need:
europe['spain']['population']
Instructions
-Use chained square brackets to select and print out the capital of France.
-Create a dictionary, named data, with the keys 'capital' and 'population'. Set them to 'rome'
and 59.83, respectively.
-Add a new key-value pair to europe; the key is 'italy' and the value is data, the dictionary
you just built.
'''
# Dictionary of dictionaries
europe = { 'spain': { 'capital':'madrid', 'population':46.77 },
'france': { 'capital':'paris', 'population':66.03 },
'germany': { 'capital':'berlin', 'population':80.62 },
'norway': { 'capital':'oslo', 'population':5.084 } }
# Print out the capital of France
print(europe['france']['capital'])
# Create sub-dictionary data
data = {'capital': 'rome', 'population': 59.83}
# Add data to europe under key 'italy'
europe['italy'] = data
# Print europe
print(europe)