I have a list with dictionaries like
[{'x': 42}, {'x': 23, 'y': 5}]
and want to make sure all dicts have the same keys, with values of None
if the key was not present in the original dict. So the list above should become
[{'x': 42, 'y': None}, {'x': 23, 'y': 5}]
What's the most beautiful and pythonic way to do this? Current approach:
keys = reduce(lambda k, l: k.union(set(l)), [d.keys() for d in my_list], set())
new_list = [dict.fromkeys(keys, None) for i in xrange(len(my_list))]
for i, l in enumerate(my_list):
new_list[i].update(l)
But especially the first two lines seem kind of clumsy. Ideas?
This creates a new list of dictionaries, all of them with complete keys:
The easiest way to do this:
Giving us:
We make a set from all the keys in all the dictionaries, then we loop through the
dict
s updating with any values they don't have.An alternative to using
itertools.chain.from_iterable()
would be be to doreduce(or_, [dict.keys() for dict in dicts])
, usingfunctools.reduce()
(in 3.x, thereduce()
builtin in 2.x) andoperator.or_
, although I feel this is less readable.If you wanted to create a new list, rather than updating the old one, simply replace the for loop with: