I have a dictionary of dictionary as follows:
lst1 = {1: {"a": "x","b": "y"}, 2: {"b": "y", "c": "z"}}
I want to make a flat dict out of it. Any duplicate keys should be removed, so that only keys unique to one of the nested dicts are present in the flattened dict. The dict above should become:
{"a": "x", "c": "z"}
the simple code would be:
for key, value in lst1.iteritems():
for key1, value1 in value.iteritems():
if key1 <> "b":
lst2[key1]=value1
I tried some of previous answers here, here and here but couldn't get it to work right.
The below code returns error: the value is not defined
lst2 = {key1: value1 for key1, value1 in value for key, value in lst1.items()}
This one:
lst2 = dict((key1, value1) for key1, value1 in (value for key, value in lst1.items()))
returns:
{'a': 'b', 'c': 'b'}
How can I flatten the structure correctly as I've described?