Use a defaultdict instead of a sql query