I have the following data
data={
None: [
{'ne': '1', 'na': '1'},
{'ne': '2', 'na': '2'},
{'ne': '3', 'na': '3'},
{'ne': '4', 'na': '4'}
],
'AO': [
{'ne': '2', 'na': '2'},
{'ne': '6', 'na': '6'}
],
'NZ': [
{'ne': '1', 'na': '1'}
]
}
and I want to have a list from it like this:
[
{'ne': '1', 'na': '1', 'country': [None, 'NZ']},
{'ne': '2', 'na': '2', 'country': [None, 'AO']},
{'ne': '3', 'na': '3', 'country': [None]},
{'ne': '4', 'na': '4', 'country': [None]},
{'ne': '6', 'na': '6', 'country': ['AO']}
]
my code is doing it fine but it's far from being "pythonic" because I'm a newbie at python:
data = {None: [{'ne': '1', 'na': '1'}, {'ne': '2', 'na': '2'}, {'ne': '3', 'na': '3'}, {'ne': '4', 'na': '4'}], 'AO': [{'ne': '2', 'na': '2'}, {'ne': '6', 'na': '6'}], 'NZ': [{'ne': '1', 'na': '1'}]}
data_list = []
for k,d in data.items():
for dd in d:
dd['country'] = k
data_list.append(dd)
help_dict = {}
for item in data_list:
help_dict[item['ne']] = False
final_list = []
for idx, val in enumerate(data_list):
if not help_dict[val['ne']]:
val['country'] = [val['country']]
for idx2, val2 in enumerate(data_list):
if idx2 != idx and val['ne'] == val2['ne']:
val['country'].append(val2['country'])
help_dict[val['ne']] = True
final_list.append(val)
print(final_list)
can someone help me with a better way to do this?
See Question&Answers more detail:os