In the following code snippet:
for record in records:
max = record.get_values()['max']
In this code, I am getting max value from record. "records" is a generator object here. The problem is, in some cases max field won't be present. So I am getting KeyError in those cases. I want to assign max = 0 if max isn't present in record. So far I have come with this solution:
try:
max = record.get_values()['max']
except:
max = 0
But I want to avoid using try-except in this case as there are many more lines like this and it will make my code look messy. How can I achieve the same functionality without using try-except or long lines of code? It will be preferable if this can be done in a single line of code.
record.get_values()returns a dictionary then you could usemax = record.get_values().get("max", 0)