I have a random function that assigns values only to some of the dict keys, but i cant run my code with others undefined. Is there a way to set a default value if the variable isnt defined?
One way i thought about solving this is assigning each key in dict None
value and then update the dict with defined variables. Is that the only way?
defined_variable = 'value'
dictionary = {
'key1': defined_variable,
'key2': undefined_varible # gives me NameError
}
print(dictionary)
# --- maybe something like this?
defined_variable = 'value'
dictionary = {
'key1': defined_variable,
try:
'key2': undefined_varible
except NameError:
# code
}
print(dictionary)