[Fixed]-How can I use two keys with defaultdict?

1👍

Your question isn’t totally clear, but I think you want a defaultdict which itself contains a defaultdict of lists. So:

class_details = defaultdict(lambda: defaultdict(list))

Alternatively, you may not need a nested dict at all; you could instead use the original defauldict with key that is a tuple:

class_details[(cls.day, cls.period)].append(cls)

Leave a comment