-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep-4-dicts.py
More file actions
112 lines (81 loc) · 2.58 KB
/
Copy pathstep-4-dicts.py
File metadata and controls
112 lines (81 loc) · 2.58 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Everything after the hash symbol is a comment
# Comments continue until the end of the line
# A dict ('dictionary') is a list of keys with associated values
my_dict = {
'a': 'aardvark',
'key': 'value'
}
# (it's basically a list of pairs, the key and value separated
# by a colon ':' and each pair separated by a comma ','
online_dict = {'a': 'aardvark', 'b': 'baal'}
# (It should look VERY familiar to users of Javascript)
# We access the value using [] syntax
print my_dict['key']
# We can add new key / values pairs to after the initial
# declaration
my_dict['hello'] = 'world'
print 'hello'
# A key can be a string, a number, or even a tuple:
my_dict[510] = 'AA55'
print my_dict[510]
my_dict[(1, 2)] = 'a value'
print my_dict[(1, 2)]
# Values can be _anything_ at all in the language
# Other containers are fine too, including other dicts
multi_dict = {
'list': [1, 2, 3, 4],
'dict': {'key': 'value'}
}
# This means you can create nested structures:
nested_dict = {
'result': {
'error_code': 0,
'text': 'This is an example',
'data': {
'news-items': [1, 2, 3],
'titles': ['test', 'titles'],
}
}
}
# Let's get news item number 2
print nested_dict['result']['data']['news-items'][1]
# And you're not just limited to data either, how about code?
# Create an empty dict
func_dict = {}
# Create a function that says 'hello'
def say_hello():
print 'hello!'
# Assign the function to a key in the dict
func_dict['my_function'] = say_hello
func_dict['my_function']()
# We test to see if a key is set with 'in':
'hello' in my_dict
'I am not a key' in my_dict
# We can remove a key with the 'del' keyword
del my_dict['hello']
print my_dict['hello']
# (Note how that threw an error - we have several ways
# of doing a 'safe' fetch from a dict)
if 'hello' in my_dict:
value = my_dict['hello']
else:
value = 'default value'
# (single line version of the above)
value = my_dict['hello'] if 'hello' in my_dict else 'default_value'
# Exception handling:
try:
value = my_dict['hello']
except KeyError:
value = 'default value'
# Better still, use the 'get' method on dicts
value = my_dict.get('hello', 'default_value')
# We can iterate over all keys and values very easily
for key, value in my_dict.items():
print 'Key value pair: %s=%s', (key, value)
# Or just iterate over the keys
for key in my_dict:
print key
# Ssh... a little secret
# Dicts are used by Python itself to store values on classes, functions and
# modules. You can do some scary stuff playing with these, but that is
# beyond the scope of today