How to initialize defaultdict with keys?

Question:

I have a dictionary of lists, and it should be initialized with default keys. I guess, the code below is not good (I mean, it works, but I don’t feel that it is written in the pythonic way):

d = {'a' : [], 'b' : [], 'c' : []}

So I want to use something more pythonic like defaultict:

d = defaultdict(list)

However, every tutorial that I’ve seen dynamically sets the new keys. But in my case all the keys should be defined from the start. I’m parsing other data structures, and I add values to my dictionary only if specific key in the structure also contains in my dictionary.

How can I set the default keys?

Asked By: Amir

||

Answers:

That’s already reasonable but you can shorten that up a bit with a dict comprehension that uses a standard list of keys.

>>> standard_keys = ['a', 'b', 'c']
>>> d1 = {key:[] for key in standard_keys}
>>> d2 = {key:[] for key in standard_keys}
>>> ...
Answered By: tdelaney

If you’re going to pre-initialize to empty lists, there is no need for a defaultdict. Simple dict-comprehension gets the job done clearly and cleanly:

>>> {k : [] for k in ['a', 'b', 'c']}
{'a': [], 'b': [], 'c': []}
Answered By: Raymond Hettinger

You can have a function defined which will return you a dict with preset keys.

def get_preset_dict(keys=['a','b','c'],values=None):
    d = {}
    if not values:
        values = [[]]*len(keys)
    if len(keys)!=len(values):
        raise Exception('unequal lenghts')
    for index,key in enumerate(keys):
        d[key] = values[index]

    return d

In [8]: get_preset_dict()

Out[8]: {'a': [], 'b': [], 'c': []}

In [18]: get_preset_dict(keys=[‘a’,’e’,’i’,’o’,’u’])

Out[18]: {'a': [], 'e': [], 'i': [], 'o': [], 'u': []}

In [19]:
get_preset_dict(keys=['a','e','i','o','u'],values=[[1],[2,2,2],[3],[4,2],[5]])

Out[19]: {'a': [1], 'e': [2, 2, 2], 'i': [3], 'o': [4, 2], 'u': [5]}

Answered By: harshil9968

If you have a close set of keys ([‘a’, ‘b’, ‘c’] in your example)
you know you’ll use, you can definitely use the answers above.

BUT…

dd = defaultdict(list) gives you much more then: d = {'a':[], 'b':[], 'c':[]}.
You can append to “not existing” keys in defaultdict:

>>dd['d'].append(5)
>>dd
>>defaultdict(list, {'d': 5})

where if you do:

>>d['d'].append(5)  # you'll face KeyError
>>KeyError: 'd'

Recommend to do something like:

>>d = {'a' : [], 'b' : [], 'c' : []}
>>default_d = defaultdict(list, **d)

now you have a dict holding your 3 keys: [‘a’, ‘b’, ‘c’] and empty lists as values, and you can also append to other keys without explicitly writing: d['new_key'] = [] before appending

Answered By: Aaron_ab

From the comments, I’m assuming you want a dictionary that fits the following conditions:

  1. Is initialized with set of keys with an empty list value for each
  2. Has defaultdict behavior that can initialize an empty list for non-existing keys

@Aaron_lab has the right method, but there’s a slightly cleaner way:

d = defaultdict(list,{ k:[] for k in ('a','b','c') })
Answered By: rovyko
from collections import defaultdict
list(map((data := defaultdict(list)).__getitem__, 'abcde'))
data

Out[3]: defaultdict(list, {‘a’: [], ‘b’: [], ‘c’: [], ‘d’: [], ‘e’:
[]})

Answered By: mcstarioni