0
I’m a beginner, guys. Just for the record!
I am trying to compare the items on a list with dictionaries and if they are equal, add these items. I think with one example, the idea becomes clearer:
dados = [
{"Codigo": 1, "Valor": 300.00,},
{"Codigo": 1, "Valor": 300.00,},
{"Codigo": 2, "Valor": 400.00,},
{"Codigo": 1, "Valor": 300.00,},
{"Codigo": 2, "Valor": 400.00,},
{"Codigo": 2, "Valor": 400.00,}
]
print(f'Número de itens: {len(dados)}')
for dado in dados:
print(f"{dado['Codigo']} = {dado['Valor']}")
The question is: If the values of Codigo
are equal - How to sum these values in an item and return only the total sum?
Thank you for your attention!
Thank you for coming back, Miguel. The intention is pedagogical. I’m studying lists and dictionaries in Python and trying to increase the difficulty level of the exercises to fix learning. I tried to do with the enumerate (I think that’s the name), but he added and replicated my added values (I made a mistake in the logic of the condition). Anyway, I’ll study what you sent me, okay? Thanks!
– MMelo
@Mmelo, enumerate is unnecessary for this purpose. Good studies
– Miguel