0

dict = { "numbers1" : [4,5,6,3,2] , "numbers2" : [40,5,63,2] , "numbers3" : [10,25,96,3]}

here how can I get the average of each key.

and if I wanted to sort the dictionary (after doing the operation on values) according to the key how can I do that

3 Answers3

1

If you change your dict to contains list of values, you can try:

data={ "numbers1" : [4,5,6,3,2] , "numbers2" : [40,5,63,2] , "numbers3" : [10,25,96,3]}
dict_Result={}
for key in data:
    meanPerKey=sum(data[key])/len(data[key])
    dict_Result[key]=meanPerKey

print(dict_Result)

result:

{'numbers1': 4.0, 'numbers2': 27.5, 'numbers3': 33.5}
Renaud
  • 2,709
  • 2
  • 9
  • 24
1

Try this

d1 = { "numbers1" : [4,5,6,3,2] , "numbers2" : [40,5,63,2] , "numbers3" : [10,25,96,3]}

d2 = {}
for a,b in d1.items():
    d2[a] = sum(b)/len(b)
# SORTING THE DICT
d2 = {key:value for key,value in sorted(d2.items(),key=lambda e: e[1])}
print(d2)

OR

d = { "numbers1" : [4,5,6,3,2] , "numbers2" : [40,5,63,2] , "numbers3" : [10,25,96,3]}

d2 = {a:sum(b)/len(b) for a,b in d.items()}
# SORTING THE DICT
d2 = {key:value for key,value in sorted(d2.items(),key=lambda e: e[1])}
print(d2)

OUTPUT {'numbers1': 4.0, 'numbers2': 27.5, 'numbers3': 33.5}

codester_09
  • 5,622
  • 2
  • 5
  • 27
1

Assuming you meant for each value in the dictionary to be a list of numbers, here's a way to do what you're asking:

d = { "numbers1" : [4,5,6,3,2] , "numbers2" : [40,5,63,2] , "numbers3" : [10,25,96,3]}
avg = {k: sum(v) / len(v) for k, v in d.items()}
print(avg)

sortedAvg = sorted(d.items(), key=lambda x: sum(x[1]) / len(x[1]))
print(sortedAvg)

Output:

{'numbers1': 4.0, 'numbers2': 27.5, 'numbers3': 33.5}
[('numbers1', [4, 5, 6, 3, 2]), ('numbers2', [40, 5, 63, 2]), ('numbers3', [10, 25, 96, 3])]

Explanation:

  • avg uses a dict comprehension to create a dictionary with the same keys as the input containing the average of each key's list
  • sortedAvg uses a list comprehension to create a list of tuples of key/value pairs in the input dict sorted in the order of ascending average.
constantstranger
  • 9,176
  • 2
  • 5
  • 19
  • can you please elaborate the work of lambda in sortedAvg – Sayandeep Banerjee Jun 27 '22 at 14:46
  • 1
    Sure. By default, the [`sorted()`](https://docs.python.org/3/library/functions.html#sorted) built-in function gives a result in ascending order of the values in the iterable argument. When we pass `lambda x: sum(x[1]) / len(x[1])` as the `key` argument, we instead cause `sorted()` to call that function to extract a comparison key from each item in the iterable. Our lambda returns the mean of the list found in `x[1]` where `x` is an item in `d.items()`, which is a sequence of `key, value` tuples from the dict `d`. So the lambda causes `sorted()` to sort the `key, list` tuples by list average. – constantstranger Jun 27 '22 at 17:15
  • Do you need any additional help with your question? – constantstranger Jun 29 '22 at 03:58
  • Good to hear. Please consider marking one of the answers as "accepted". – constantstranger Jun 30 '22 at 12:02