I have a list of dictionaries where all dicts contain similar items but some dicts are missing certain items. Here's an example of what it would look like:
data = [
{
'city' : Toronto,
'colour' : blue
},
{
'city' : London,
'country' : UK
'colour' : green,
'name' : Alex
},
{
'city' : Kingston,
'colour' : purple,
'name' : Alex
}
]
I need to match the format of the largest dict by inserting items (with the same keys but blank values) into the smaller dicts. I also need to preserve the order of the keys so I can't just insert them at the end. Following the previous example, it would look like this:
data = [
{
'city' : Toronto,
'country' : ,
'colour' : blue,
'name' :
},
{
'city' : London,
'country' : UK
'colour' : green,
'name' : Alex
},
{
'city' : Kingston,
'country' : ,
'colour' : purple,
'name' : Alex
}
]
I'm not sure how to loop through and add entries to each dict since the dicts I'm comparing are different size. I've tried copying the largest dict and editing it, adding blank values to the end of each dict and reformatting those, and creating new dicts as I loop through but nothing has worked so far. Here is my code so far (where all_keys is a list of all the keys in the correct order).
def format_data(input_data, all_keys)
formatted_list = [{} for i in range(len(input_data)) ]
increment = 0
for i in range(len(input_data)):
for key, value in input_data[i].items():
if (key == all_keys[increment]):
formatted_list[i][increment].update(all_keys[increment], ''))
else:
formatted_list[i][inrement].update(key, value)
increment += 1
increment = 0
return formatted_list
How can I format this? Thanks!
Dictionaries are considered unordered (unless you are using Python 3.7+). If you need a specific order, this must be specified explicitly.
For Python <3.7, you can use collections.OrderedDict
: there's no concept of "inserting into the middle of a dictionary".
The example below uses set.union
to calculate the union of all keys; and sorted
to sort keys alphabetically.
from collections import OrderedDict
keys = sorted(set().union(*data))
res = [OrderedDict([(k, d.get(k, '')) for k in keys]) for d in data]
Result:
print(res)
[OrderedDict([('city', 'Toronto'),
('colour', 'blue'),
('country', ''),
('name', '')]),
OrderedDict([('city', 'London'),
('colour', 'green'),
('country', 'UK'),
('name', 'Alex')]),
OrderedDict([('city', 'Kingston'),
('colour', 'purple'),
('country', ''),
('name', 'Alex')])]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments