0

I have a huge List of dictionary like this which has almost 100 entries.

[{ "color":-65536, 
  "touch_size":0.21960786,
  "touch_x":831.25,
  "touch_y":1597.2656
},
{ "color":-65536,
  "touch_size":0.20392159,
  "touch_x":1302.5,
  "touch_y":1496.0938
}, .... {}]

I want to insert 2 new keys with values in each dictionary on a particular position.

new_keys = ['touch_x_dp','touch_y_dp']

touch_x_dp needs to be placed after key touch_x and touch_y_dp needs to be placed after touch_y The values for these need to be initialized with None.

I have tried this but this does not place them where I need.

for key in dp_keys:data['attempts'][i]["items"][j][key]=None
Gabio
  • 9,126
  • 3
  • 12
  • 32
LalaLand
  • 139
  • 3
  • 14
  • 3
    can you explain why you need this in a particular order – Ragul M B May 12 '20 at 11:18
  • 6
    Dictionaries were unordered pre- Python 3.7 and are sorted by order of insertion in newer versions, so you might have to use `OrderedDict` depending on your version and will need to reconstruct the dictionaries in either case. – meowgoesthedog May 12 '20 at 11:18
  • 1
    Does this answer your question? [Insert key-value pair into dictionary at a specified position](https://stackoverflow.com/questions/44390818/insert-key-value-pair-into-dictionary-at-a-specified-position) – deadshot May 12 '20 at 11:20
  • @Rahul I need to printout the values of these keys into a excel using the xlsx writer. – LalaLand May 12 '20 at 11:28
  • 1
    For you apparent use-case, [named tuples](https://stackoverflow.com/q/2970608/4996248) might be preferable to dictionaries. – John Coleman May 12 '20 at 11:28

2 Answers2

1

If your Python is 3.7+, you can do the following:

data = [{"color": -65536,
         "touch_size": 0.21960786,
         "touch_x": 831.25,
         "touch_y": 1597.2656
         },
        {"color": -65536,
         "touch_size": 0.20392159,
         "touch_x": 1302.5,
         "touch_y": 1496.0938
         }]

fields_order = ["color", "touch_size", "touch_x", "touch_x_dp", "touch_y", "touch_y_dp"]
payload = []
for d in data:
    entity = dict([(f, d.get(f)) for f in fields_order])
    payload.append(entity)

print(payload)
# output: [{'color': -65536, 'touch_size': 0.21960786, 'touch_x': 831.25, 'touch_x_dp': None, 'touch_y': 1597.2656, 'touch_y_dp': None}, {'color': -65536, 'touch_size': 0.20392159, 'touch_x': 1302.5, 'touch_x_dp': None, 'touch_y': 1496.0938, 'touch_y_dp': None}]

For Python<3.7, you should use OrderedDict:

from collections import OrderedDict

fields_order = ["color", "touch_size", "touch_x", "touch_x_dp", "touch_y", "touch_y_dp"]
payload = []
for d in data:
    entity = OrderedDict([(f, d.get(f)) for f in fields_order])
    payload.append(entity)

print(payload)
Gabio
  • 9,126
  • 3
  • 12
  • 32
-3

There is no buildin method to put some key in some order in python dicts. You can write your own function that resigns every key after inserting but in general, it is not a good idea to rely on keys orders.