Skip to content
Advertisement

accessing nested dictionary, that holds empty entries in python with get method and for loop that

Assume you’ve got the following dictionary:

JavaScript

All good with that

JavaScript

But if some of the keys don’t hold a threshold section I got the keyError.

JavaScript

The keyError as often described comes.

I tried to solve it this way:

JavaScript

But key is not known then. How would you solve it.

Advertisement

Answer

One option is to add a check if 'thresholds' exists

JavaScript

Using get() you can do something like

JavaScript

however this will leave an empty dictionary in thresholds.

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