'Average the values of a list of dictionaries
I have the following list of dictionaries. Each dictionary has a "Point" and a "Value" and goes from 1 to 10, for each series of points.
My_list = [{"Point": 1, "Value": 40}, {"Point": 2, "Value": 40}, {"Point": 3, "Value": 40}, \
{"Point": 4, "Value": 40}, {"Point": 5, "Value": 40}, {"Point": 6, "Value": 40}, \
{"Point": 7, "Value": 40}, {"Point": 8, "Value": 40}, {"Point": 9, "Value": 0},{"Point": 10, "Value": 250},\
{"Point": 1, "Value": 40}, {"Point": 2, "Value": 40}, {"Point": 3, "Value": 40}, \
{"Point": 4, "Value": 40}, {"Point": 5, "Value": 40}, {"Point": 6, "Value": 40}, \
{"Point": 7, "Value": 40}, {"Point": 8, "Value": 40}, {"Point": 9, "Value": 0},{"Point": 10, "Value": 250},\
{"Point": 1, "Value": 40}, {"Point": 2, "Value": 40}, {"Point": 3, "Value": 40}, \
{"Point": 4, "Value": 40}, {"Point": 5, "Value": 40}, {"Point": 6, "Value": 40}, \
{"Point": 7, "Value": 40}, {"Point": 8, "Value": 40}, {"Point": 9, "Value": 0},{"Point": 10, "Value": 250}]
I would like to find the average 'Value' for every 2 'Point', without messing with the 'Value' of the next series. I have done the following.
every2 = []
counter = 2
temp = []
for point in My_list:
if counter > 0:
temp.append(point["Value"])
else:
p = point
p["Value"] = sum(temp)/len(temp)
every2.append(point)
# reset the counter after every 2 point
counter = 2
temp = []
# temp.append(point["Value"])
counter -= 1
print(every2)
The result I am getting is:
[{'Point': 3, 'Value': 40.0}, {'Point': 5, 'Value': 40.0}, {'Point': 7, 'Value': 40.0}, {'Point': 9, 'Value': 40.0},
{'Point': 1, 'Value': 250.0}, {'Point': 3, 'Value': 40.0}, {'Point': 5, 'Value': 40.0}, {'Point': 7, 'Value': 40.0},
{'Point': 9, 'Value': 40.0}, {'Point': 1, 'Value': 250.0}, {'Point': 3, 'Value': 40.0}, {'Point': 5, 'Value': 40.0},
{'Point': 7, 'Value': 40.0}, {'Point': 9, 'Value': 40.0}]
However I am missing the first 'Point', as the 'Point' of the first series starts from 3 instead of 1 and as a consequence the 'Point' 9 has a value of 40 instead of 125.
So what I want should look like this:
[{'Point': 1, 'Value': 40.0}, {'Point': 3, 'Value': 40.0}, {'Point': 5, 'Value': 40.0}, {'Point': 7, 'Value': 40.0},
{'Point': 9, 'Value': 125.0}, {'Point': 1, 'Value': 40.0}, {'Point': 3, 'Value': 40.0}, {'Point': 5, 'Value': 40.0},
{'Point': 7, 'Value': 40.0}, {'Point': 9, 'Value': 125.0}, {'Point': 1, 'Value': 40.0}, {'Point': 3, 'Value': 40.0},
{'Point': 5, 'Value': 40.0}, {'Point': 7, 'Value': 40.0}, {'Point': 9, 'Value': 125.0}]
Solution 1:[1]
You can also use list comprehension
res = [{**data[n], **{'Value': sum(v['Value']/2 for v in data[n: n+2])}} for n in range(0, len(data), 2)]
print(res)
Output:
[{'Point': 1, 'Value': 40.0},
{'Point': 3, 'Value': 40.0},
{'Point': 5, 'Value': 40.0},
{'Point': 7, 'Value': 40.0},
{'Point': 9, 'Value': 125.0},
{'Point': 1, 'Value': 40.0},
{'Point': 3, 'Value': 40.0},
{'Point': 5, 'Value': 40.0},
{'Point': 7, 'Value': 40.0},
{'Point': 9, 'Value': 125.0},
{'Point': 1, 'Value': 40.0},
{'Point': 3, 'Value': 40.0},
{'Point': 5, 'Value': 40.0},
{'Point': 7, 'Value': 40.0},
{'Point': 9, 'Value': 125.0}]
For python 3.9+
[data[n] | {'Value': sum(v['Value']/2 for v in data[n: n+2])} for n in range(0, len(data), 2)]
Solution 2:[2]
Here's another option that's using zip to "parallel loop" over My_list with an offset:
result = [
{"Point": p1["Point"], "Value": (p1["Value"] + p2["Value"]) / 2}
for p1, p2 in zip(My_list[::2], My_list[1::2])
]
Requirement is that all the series have even length.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | deadshot |
| Solution 2 | Timus |
