-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path06_dicts.py
More file actions
27 lines (19 loc) · 848 Bytes
/
06_dicts.py
File metadata and controls
27 lines (19 loc) · 848 Bytes
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
# Dict operations
a_dict = {'first_name': 'Minor', 'names_count': 2}
# Accessing a dict
print 'Complete dict:', a_dict
print 'Length of the dict:', len(a_dict)
print 'Value for key first_name:', a_dict['first_name']
print 'Value for key names_count:', a_dict['names_count']
print 'Keys:', a_dict.keys() # List; no guaranteed order
print 'Values:', a_dict.values() # List; no guaranteed ord
print 'Keys and value items:', a_dict.items() # List of tuples; no guaranteed order
print
# Modifying a dict
print 'dict before modification:', a_dict
del a_dict['first_name'] # Delete the mapping from 'first_name': 'Minor'
print 'dict after deleting first_name:', a_dict
a_dict['key3'] = True # Add a new mapping
print 'dict with key3 added:', a_dict
# Useful idiom
a_dict.setdefault('key4', 4) # Set key4 to 4 iff it does not contain a key4 already