+1 vote
in Programming Languages by (59.4k points)
I have a dictionary of dictionaries. I want a Python code to get a set of unique keys present in the inner dictionaries of my dictionary of dictionaries.

E.g.

aa={'a':{'x':1, 'y':2, 'z':2}, 'b':{'x':3, 't':2, 'z':9, 'u':5}}

result should be

{'y', 'z', 'u', 'x', 't'}

How can I do it in a Pythonic way?

1 Answer

+1 vote
by (353k points)
 
Best answer

You can use the following one-liner Pythonic code to get unique keys from the inner dictionaries:

>>> aa={'a':{'x':1, 'y':2, 'z':2}, 'b':{'x':3, 't':2, 'z':9, 'u':5}}

>>> aa

{'a': {'x': 1, 'y': 2, 'z': 2}, 'b': {'x': 3, 't': 2, 'z': 9, 'u': 5}}

>>> {v for vals in aa.values() for v in vals}

{'y', 'z', 'u', 'x', 't'}

>>> 


...