Creating

# empty dict
my_dict = {}
# integer keys
my_dict = {1: "a", 2: 3}
# contains complicated types
my_dict = {1: ["1", "2"], "2": {1: 1, 2: 2}, 3: (1, 2)}

Access elements

my_dict = {1: "a", 2: "b"}
my_dict[1]
my_dict.keys()
my_dict.values()
my_dict.items()
'a'
dict_keys([1, 2])
dict_values(['a', 'b'])
dict_items([(1, 'a'), (2, 'b')])
for key, val in my_dict.items():
  print(key, val)
1 a
2 b

If the key doesn’t exist, use the default!

dct = {1: 'a', 2: 'b'}
dct.get(1)
dct.get(3, 'c')
'a'
'c'

Sorted keys

for key in sorted(my_dict.keys()):
  pass