Currently I am using the following method, assume dictionary
data[a][b][c]
I use:
if "a" in data and "b" in data["a"] and "c" in data["a"]["b"]:
...
Are there any better way?
You can wrap it in a try/except block
try:
x = data[a][b][c]
...do stuff in your "if" clause
except KeyError:
...do stuff in your "else" clause
I would typically use the pattern
foo = data.get("a",{}).get("b",{}).get("c",False)
if foo:
...
This requires that the nesting is always the same depth and that data["a"]["b"]["c"] is not "falsy".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With