So let's say I have AllData, a list of nested dicts (let's pretend there are thousands of entries) but I'm only looking for a single entry.
AllData = [
{ "EntryA" : {"A Key":"A Value"} },
{ "EntryB" : {"B Key":"B Value"} },
{ "EntryC" : {"C Key":"C Value"} }
]
Currently I'm just looping through it and breaking once I find the matching key.
MyKey = "EntryB"
FoundData = {}
for item in AllData:
for key,data in item.items():
if MyKey.lower() == key.lower():
FoundData = data
break
print(FoundData)
However, being a Python enthusiast I know there's a more elegant way to do this. I've struggled a bit with proper list comprehension techniques but I'm trying! Below is the best I've been able to come up with that gathers this data in a single line but I'm wondering if there is an even MORE elegant solution to this problem.
MyKey = "EntryB"
FoundData = [x for x in AllData if str(list(x.items())[0][0]).lower() == MyKey.lower()][0][MyKey]
print(FoundData)
Any insight would be greatly appreciated :)