0

I have a list with multiple dictionaries inside it.

Here is my list:

[{'clicks': 1.2195121951219512, 'conversions': 1.4227642276422763, 'cpc': 2.2357723577235773, 'cpm': 4.471544715447155, 'reach': 90.65040650406505}, 
{'clicks': 1.2048192771084338, 'conversions': 1.4056224899598393, 'cpc': 2.208835341365462, 'cpm': 5.622489959839357, 'reach': 89.5582329317269}]

My end goal is to make a single dict with an average from the above dicts.

Example:

[{'clicks': 1.2048192771084338, 'conversions': 1.4056224899598393, 'cpc': 2.208835341365462, 'cpm': 5.622489959839357, 'reach': 89.5582329317269}]

My problem is: how do I access and compare the keys from multiple dicts at a time? Is there a better way to do it?

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
  • what do u mean average among them? do u want to create a new dictionary containing their averages of their respective values in the above given list of dictionaries? – Ghost Ops Sep 19 '21 at 11:57
  • yes, I need to create a new dictionary with average of the values from the above list. eg: "cpc" from both the dictionary in the list and then average them out @GhostOps – Shubham Sangle Sep 19 '21 at 11:59
  • How do you want to compare? – Dejene T. Sep 19 '21 at 11:59
  • using keys. Both the list in the example have CPC, Conversions keys in them. @DejeneT. – Shubham Sangle Sep 19 '21 at 12:02
  • 1
    I think this answers your question https://stackoverflow.com/questions/34139512/average-value-in-multiple-dictionaries-based-on-key-in-python – Dejene T. Sep 19 '21 at 12:05
  • @DejeneT. I can use the link you send me but in that question, they have separate dicts, unlike mine that has a list with dicts in it – Shubham Sangle Sep 19 '21 at 12:11
  • Your example result doesn't look like an average of the two input dicts. It's just the same as the second input dict. – no comment Sep 19 '21 at 12:32

3 Answers3

1

Try this...

lst = [{'clicks': 1.2195121951219512, 'conversions': 1.4227642276422763, 'cpc': 2.2357723577235773, 'cpm': 4.471544715447155, 'reach': 90.65040650406505},
       {'clicks': 1.2048192771084338, 'conversions': 1.4056224899598393, 'cpc': 2.208835341365462, 'cpm': 5.622489959839357, 'reach': 89.5582329317269}]

res = {}
keys_ = ['clicks', 'conversions', 'cpc', 'cpm', 'reach']
for j in keys_:
    temp = [i[j] for i in lst]
    res[j] = sum(temp)/len(temp)

print(res)

Or you can also try this One-line code

print({j:sum([i[j] for i in lst])/len([i[j] for i in lst]) for j in lst[0].keys()})

Tell me if its not working...

Ghost Ops
  • 1,710
  • 2
  • 13
  • 23
1

If all dictionaries in the list have the same keys, you can use:

from statistics import mean

lst = [
    {
        "clicks": 1.2195121951219512,
        "conversions": 1.4227642276422763,
        "cpc": 2.2357723577235773,
        "cpm": 4.471544715447155,
        "reach": 90.65040650406505,
    },
    {
        "clicks": 1.2048192771084338,
        "conversions": 1.4056224899598393,
        "cpc": 2.208835341365462,
        "cpm": 5.622489959839357,
        "reach": 89.5582329317269,
    },
]

out = {k: mean(d[k] for d in lst) for k in lst[0]}
print(out)

Prints:

{
    "clicks": 1.2121657361151925,
    "conversions": 1.4141933588010578,
    "cpc": 2.2223038495445193,
    "cpm": 5.047017337643256,
    "reach": 90.10431971789598,
}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 1
    this worked for me. Although I cannot use the mean library because I need to learn to do it manually for my task, this helped me out and i learnt a new library. Thanks a lot :) – Shubham Sangle Sep 19 '21 at 12:27
1

I hope the following code will help you to get understand how to find the average values of multiple lists of dict items

# your list of dict items
dlist = [{'clicks': 1.2195121951219512, 'conversions': 1.4227642276422763, 'cpc': 2.2357723577235773, 'cpm': 4.471544715447155, 'reach': 90.65040650406505}, 
{'clicks': 1.2048192771084338, 'conversions': 1.4056224899598393, 'cpc': 2.208835341365462, 'cpm': 5.622489959839357, 'reach': 89.5582329317269}]
newdict = {}
for d in dlist:
    for k, v in d.items():
        if k in newdict:
            newdict[k].append(v)
        else:
            newdict[k] = [v]
            
# find average from the new dict
for key, value in newdict.items():
    newdict[key] = sum(value)/len(value)
print(newdict)

output

{'clicks': 1.2121657361151925,
 'conversions': 1.4141933588010578,
 'cpc': 2.2223038495445193,
 'cpm': 5.047017337643256,
 'reach': 90.10431971789598}
Dejene T.
  • 973
  • 8
  • 14