Skip to content
Advertisement

How can I add a new pair key: value in a dictionary that has a condition?

I have the following dictionary:

JavaScript

The keys are the protocols numbers (‘2019.491429’, ‘2020.714034’, ‘2015.695102’, etc.) while the values are a subdictionary which has 2 keys (‘classe’ and ‘dias_vista’).
I want to create a new pair “prorrogado”: “sim” for the keys which have the value from “dias_vista” equal or above 10 and for the others “prorrogado”: “não”.

I have tried using this loop method:

JavaScript

But it returned "TypeError: 'dict' object is not callable". How can I solve it?

Advertisement

Answer

Just do:

JavaScript

Output

JavaScript

The use of update is preferred for multiple keys or another dictionary.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement