using defaultdict(list), you can simplify the logic here.