Skip to content

Instantly share code, notes, and snippets.

@VinACE
Last active May 14, 2019 17:28
Show Gist options
  • Save VinACE/8aa5aba5d38ce90ebd71caf5220d5a9c to your computer and use it in GitHub Desktop.
Save VinACE/8aa5aba5d38ce90ebd71caf5220d5a9c to your computer and use it in GitHub Desktop.
https://stackoverflow.com/questions/50970824/conversion-of-dict-object-into-a-list-of-dict-object
d = {'Attributes': {'Fifth': 'blind (19.33%)',
'First': 'Art (40.0%)',
'Fourth': 'Ser (20.0%)',
'Second': 'Nat (21.33%)',
'Third': 'per (20.67%)'}}
order = {v: k for k, v in enumerate(('First', 'Second', 'Third', 'Fourth', 'Fifth'))}
sorter = sorted(d['Attributes'].items(), key=lambda x: order[x[0]])
L = [dict(enumerate({k: v} for k, v in sorter))]
print(L)
[{0: {'First': 'Art (40.0%)'},
1: {'Second': 'Nat (21.33%)'},
2: {'Third': 'per (20.67%)'},
3: {'Fourth': 'Ser (20.0%)'},
4: {'Fifth': 'blind (19.33%)'}}]
list_res = []
for in in d['Attributes']:
a = (str(i):(d['Attributes'][i])})
list_res.append(a)
print(list_res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment