Creating a dictionary from two lists? While taking an average of values?

Viewed 274

Wizards of stackoverflow,

I wish to combine two lists to create a dictionary, I have used dict & zip, however it does not meet what I require.

If had these lists

  keys = ['a', 'a', 'b', 'c']
values = [6, 2, 3, 4]

I would like for the dictionary to reflect the average value such that the output would be:

a_dict = {'a' : 4, 'b' : 3, 'c' : 4}

as a bonus but not required, if this is possible is there anyway to get a count of each duplicate? i.e. output would be followed by 'a' was counted twice, other than just doing the count in the keys.

3 Answers

A straightforward solution (thanks @DeepSpace for dict-comprehension suggestion):

keys = ['a', 'a', 'b', 'c']
values = [6, 2, 3, 4]

out = {}
for k, v in zip(keys, values):
    out.setdefault(k, []).append(v)

out = {key: sum(value) / len(value) for key, value in out.items()}

print(out)

Prints:

{'a': 4.0, 'b': 3.0, 'c': 4.0}

If you want count of keys, you can do for example:

out = {}
for k, v in zip(keys, values):
    out.setdefault(k, []).append(v)

out = {key: (sum(value) / len(value), len(value)) for key, value in out.items()}

print(out)

Prints:

{'a': (4.0, 2), 'b': (3.0, 1), 'c': (4.0, 1)}

Where the second element of values is a count of key.


Solution with itertools (if keys are sorted):

keys = ['a', 'a', 'b', 'c']
values = [6, 2, 3, 4]

from itertools import groupby
from statistics import mean

out = {}
for k, g in groupby(zip(keys, values), lambda k: k[0]):
    out[k] = mean(v for _, v in g)

print(out)

Prints:

{'a': 4, 'b': 3, 'c': 4}

calculating avg and frequency of each key dic = {key: [avg, frequency]}

keys = ['a', 'a', 'b', 'c']
values = [6, 2, 3, 4]

dic = {i:[[], 0] for i in keys}


for k, v in zip(keys, values):
    dic[k][0].append(v)
    dic[k][1]+=1

for k, v in dic.items():
    dic[k][0] = sum(dic[k][0])/len(dic[k][0])

print(dic)

output

{'a': [4.0, 2], 'b': [3.0, 1], 'c': [4.0, 1]}
keys = ['a', 'a', 'b', 'c']
values = [6, 2, 3, 4]
d, count_dict=dict(), dict()
for i in range(len(keys)):
    try:
        d[keys[i]]+=values[i]
        count_dict[keys[i]]+=1
    except KeyError:
        d[keys[i]]=values[i]
        count_dict[keys[i]]=1
for keys,values in d.items():
    d[keys]=d[keys]/count_dict[keys]
    print(f'{keys} comes {count_dict[keys]} times')
print(d)
Related