-1

I have the following dict structure

d = {'Attributes': {'Fifth': 'blind (19.33%)',
                    'First': 'Art (40.0%)',
                    'Fourth': 'Ser (20.0%)',
                    'Second': 'Nat (21.33%)',
                    'Third': 'per (20.67%)'}}

Need to convert into the following structure list of dictionary items

 [   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%)'}
 ]
jpp
  • 159,742
  • 34
  • 281
  • 339
vinsent paramanantham
  • 953
  • 3
  • 15
  • 34

2 Answers2

2

First of all, the structure you want as output is not a python list format. Actually, it is not also a dictionary format either.

From your question, I've learned that you want to make a list of dictionaries.

First, make a dictionary element:

0: {'First': 'Art (40.0%)'}

to

{0: {'First': 'Art (40.0%)'}}

Then, you will be ready to make a list of a dictionary and your data structure will look like:

[   {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%)'}}
 ]

you can check the structure:

list =  [   {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%)'}}
 ]
print(type(a))
print(type(list[0]))

Output:

<class 'list'>
<class 'dict'>

And the code

dict_value = {'Attributes': {'Fifth': 'blind (19.33%)',
                    'First': 'Art (40.0%)',
                    'Fourth': 'Ser (20.0%)',
                    'Second': 'Nat (21.33%)',
                    'Third': 'per (20.67%)'}}

order = {value: key for key, value in enumerate(('First', 'Second', 'Third', 'Fourth', 'Fifth'))}

sorted_form = sorted(dict_value['Attributes'].items(), key=lambda d: order[d[0]])
final_list = [dict(enumerate({key: value} for key, value in sorted_form))]

print(final_list)

produces

[{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%)'}}]
Sudarshan
  • 938
  • 14
  • 27
  • what if my output just has to be, please let me know [{'First': 'Art (40.0%)'}, {'Second': 'Nat (21.33%)'}, {'Third': 'per (20.67%)'}, {'Fourth': 'Ser (20.0%)'}, {'Fifth': 'blind (19.33%)'}] – vinsent paramanantham Jun 25 '18 at 11:50
  • just change the `final_list` as `final_list = {item[0]:item[1] for item in sorted_form}` – Sudarshan Jun 25 '18 at 15:45
1

Your question is unclear and your desired output is not valid Python. I assume you want a list of dictionaries as your desired output. There are a few steps.

  1. Define your order. Python doesn't know the string "Fourth" should come after "Third".
  2. Apply ordering to dictionary items. Dictionaries are unordered in Python (unless you are using 3.7+).
  3. Use a comprehension with enumerate to construct your list result.

Here's a complete example.

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%)'}}]
jpp
  • 159,742
  • 34
  • 281
  • 339