Skip to content

Instantly share code, notes, and snippets.

@udhayprakash
Created September 22, 2017 16:31
Show Gist options
  • Save udhayprakash/8c2c3a7a3057c7ec50f25c1fbd3f295f to your computer and use it in GitHub Desktop.
Save udhayprakash/8c2c3a7a3057c7ec50f25c1fbd3f295f to your computer and use it in GitHub Desktop.
dictionary_reformatting.py
""""
Input :
{'fruits':{'a':'no','b':'no',c:'yes'},'vegetables':{'x':'no','y':'yes'}}
Output:
[{'fruits':{c:'yes'},'vegetables':{'y':'yes'}}]
(if value contain "no" then remove that pairs)
"""
myDict = {'fruits':{'a':'no','b':'no','c':'yes'},'vegetables':{'x':'no','y':'yes'}}
print(myDict)
newDict = {}
for key,valueDict in myDict.items():
#print key,valueDict
temp = {}
for d in valueDict:
if valueDict[d] != 'no':
temp[d] = valueDict[d]
newDict[key] = temp
print(newDict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment