How can I use dictionary comprehension for values that are literals
or list
Right now I'm able to iterate through a nested dictionary and get as a result dict with nested values, but I would like to include in the output dict
, list, and literals (int, str)
Here is my example ( I know that isinstance
is not needed here)
nested_dict = {'first':{'a':1}, 'second':{'b':2}, 'third': 3, 'fourth': [1, 2, 3, 4]}
float_dict = {
outer_k: { float(inner_v)
for (inner_k, inner_v) in outer_v.items()}
for (outer_k, outer_v) in nested_dict.items()
if isinstance(outer_v, dict)
}
print(float_dict)
Expected output:
{'first': {'a': 1.0}, 'second': {'b': 2.0}, 'third': 3.0, 'fourth': [1.0, 2.0, 3.0, 4.0]}