7

I have the following scenario - consider the list of dictionaries as below (input):

data = [{'key1' : 'k1v1', 'key2': 'k2v1'}, {'key1': 'k1v2', 'key2': 'k2v2'}, 
{'key1': 'k1v3', 'key2': 'k2v3'}, {'key1': 'k1v4', 'key2': 'k2v4'}]

As can be observed, the keys across the dictionaries are same. So what I want to be able to do is the following (desired output):

{'key1': ['k1v1', 'k1v2', 'k1v3', 'k1v4'], 'key2': ['k2v1', 'k2v2', 'k2v3', 'k2v4']}

To achieve this, I can use the following simple straight forward nested loop:

data_dict = dict()
for d in data:
    for k, v in d.items():
        if k in data_dict:
            data_dict[k].append(v)
        else:
            data_dict[k] = [v]

The question: Can we do this using dictionary comprehension? In other words, can we append to existing value lists, inside a dictionary comprehension?

anurag
  • 1,715
  • 1
  • 8
  • 28
  • I am not looking for actual performance benefit, though there may be some; I am just curious if we can achieve this using dictionary-comprehension. – anurag Jan 15 '21 at 08:10

1 Answers1

3

This is the best that I can do:

d = {}
for k in data[0].keys():
    d[k] = list(d[k] for d in ds)

Here is a solution which gets closer to dict comprehension

data_dict = {
    k: [d.get(k) for d in data]
    for k in set().union(*data)
}

The following syntax results in a code with fastest timings:

data_dict = {k: [dt[k] for dt in data] for k in data[0].keys()}

# Timing:
# 1.08 µs ± 18.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
anurag
  • 1,715
  • 1
  • 8
  • 28
itprorh66
  • 3,110
  • 4
  • 9
  • 21