Reputation: 2807
This is my sample Json data.
dict = {'Headers 1': {'sub head 1': { 'birds':['Item 1.1.1',
'Item 1.1.2',
'Item 1.1.3',
'Item 1.1.4'],
'animals':['Item 1.1.1',
'Item 1.1.2',
'Item 1.1.3',
'Item 1.1.4']
},
'sub head 2': { 'birds':['Item 1.1.1',
'Item 1.1.2',
'Item 1.1.3',
'Item 1.1.4'],
'books':['Item 1.1.1',
'Item 1.1.2',
'Item 1.1.3',
'Item 1.1.4']
},
},
'Headers 2': {'sub head 1': { 'bottles':['Item 1.1.1',
'Item 1.1.2',
'Item 1.1.3',
'Item 1.1.4'],
'animals':['Item 1.1.1',
'Item 1.1.2',
'Item 1.1.3',
'Item 1.1.4']
},
'sub head 2': { 'books':['Item 1.1.1',
'Item 1.1.2',
'Item 1.1.3',
'Item 1.1.4'],
'birds':['Item 1.1.1',
'Item 1.1.2',
'Item 1.1.3',
'Item 1.1.4']
}
},
'Headers 3': {'sub head 1': { 'bottles':['Item 1.1.1',
'Item 1.1.2',
'Item 1.1.3',
'Item 1.1.4'],
'birds':['Item 1.1.1',
'Item 1.1.2',
'Item 1.1.3',
'Item 1.1.4']
}
}
}
Here from this Json data, I want to fetch the unique set of all the Keys of sub headers.My result should be,
['animals','birds','books','bottles']
I will tell my steps that I have done so far,
Get all the Keys of dict. i.e
dict.keys()
Iterate keys one by one get each keys of Parent keys.
Iterate on sub header keyes and get all the child keys of sub headers keys.
Now I don't know how to store this child keys to a unique set in python? How can I that unique list? Could I get the list sorted?
Upvotes: 0
Views: 1064
Reputation: 3758
Can use set
. Something like:
res = set()
for v in dict.values():
for i in v.values():
res.update(set(i.keys()))
print(res) # {'birds', 'animals', 'books', 'bottles'}
On a personal note,
I find this syntax better read than the comprehension syntax. Her you follow the flow, can easy put a break-point and understand what you are doing..
Upvotes: 1
Reputation: 26315
Assuming you want the result unordered, iterate 3 levels deep and take the set()
of keys on the third level:
print(set(k for outer in d.values() for inner in outer.values() for k in inner))
# {'birds', 'books', 'animals', 'bottles'}
If you want a list instead, wrap list()
:
print(list(set(k for outer in d.values() for inner in outer.values() for k in inner)))
# ['birds', 'books', 'bottles', 'animals']
If you want to have a sorted list, wrap sorted()
:
print(sorted(set(k for outer in d.values() for inner in outer.values() for k in inner)))
# ['animals', 'birds', 'books', 'bottles']
If you want an ordered result, keep a seen set to keep track of duplicates and store the result in a list:
seen = set()
result = []
for outer in d.values():
for inner in outer.values():
for k in inner:
if k not in seen:
seen.add(k)
result.append(k)
print(result)
# ['birds', 'animals', 'books', 'bottles']
The above also doesn't care about the keys on the first two levels, so you can iterate over the dictionary values()
instead.
Note: Naming a variable dict
is not a good idea, since it shadows the builtin dict()
function.
Upvotes: 2