Python: defaultdict(list) Dictionary of Lists
By Güngör Budak
- One minute read - 96 wordsMost of the time, when you need to work on large data, you’ll have to use some dictionaries in Python. Dictionaries of lists are very useful to store large data in very organized way. You can always initiate them by initiating empty lists inside an empty dictionary but when you don’t know how many of them you’ll end up with and if you want an easier option, use defaultdict(list)
. You just need to import it, first:
1from collections import defaultdict
And then initiate anywhere:
1dict = defaultdict(list)
You can always collect data in this way:
1dict["some_id"].append("some_value")