0

I "decoded" a dictionary of dictionaries and came down to:

waycategory_values = data['features'][0]['properties']['extras']['waycategory']['summary']

which results in:

[{'value': 3.0, 'distance': 230472.2, 'amount': 96.03}, {'value': 0.0, 'distance': 8713.7, 'amount': 3.63}, {'value': 1.0, 'distance': 811.1, 'amount': 0.34}]

now I want to get the distance and add the corresponding values up, if the value = 1.0, 2.0 or 3.0 I tried getting the distance by doing the following:

for distance in waycategory_values[0:]['distance']:
    if waycategory_values[0:]['value'] == 1.0 or 2.0 or 3.0:
        print(distance)

well - that's obviously wrong.. but can't seem to figure out how to do it - if you could just give me a hint, that would be awesome!

graukas
  • 41
  • 7
  • Does this answer your question? [Why does \`a == b or c or d\` always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true) – jarmod Feb 08 '21 at 17:54

2 Answers2

1
data = [{'value': 3.0, 'distance': 230472.2, 'amount': 96.03}, {'value': 0.0, 'distance': 8713.7, 'amount': 3.63}, {'value': 1.0, 'distance': 811.1, 'amount': 0.34}]

total_distance = 0
# just go through the list, no need for indexing
for dataset in data:
    if dataset['value'] in [1.0, 2.0, 3.0]: # <- list of values to check against
        total_distance += dataset['distance']
Banana
  • 2,295
  • 1
  • 8
  • 25
1
d=[{'value': 3.0, 'distance': 230472.2, 'amount': 96.03}, {'value': 0.0, 'distance': 8713.7, 'amount': 3.63}, {'value': 1.0, 'distance': 811.1, 'amount': 0.34}]
sum(v['distance'] for v in d if v['value'] == 1 or v['value'] == 2 or   v['value'] ==  3  )

231283.30000000002  
Ajay
  • 5,267
  • 2
  • 23
  • 30