from collections import defaultdict
def new_collection():
#you could write this as a lambda to save two lines, but...
#Let's teach one thing at a time, please
return {'p': [], 'r': [], 'f': []}
ent_dict = defaultdict(new_collection)
values = ["foo", "bar", "baz", "foo", "qux"]
for idx, val in enumerate(values):
ent_dict[val]["p"].append(idx)
for key, val in ent_dict.items():
print(key, val)
#result
# foo {'p': [0, 3], 'r': [], 'f': []}
# bar {'p': [1], 'r': [], 'f': []}
# baz {'p': [2], 'r': [], 'f': []}