Here is how you can use zip()
:
countries = ['Argentina', 'Brazil', 'France']
population = ['100', '900', '1400']
ranks = ['1', '2', '3']
result_list_of_dicts = [{'country': c,
'rank': r,
'populations': p}
for c, p, r in
zip(countries, population, ranks)]
print(result_list_of_dicts)
Output:
[{'country': 'Argentina', 'rank': '1', 'populations': '100'},
{'country': 'Brazil', 'rank': '2', 'populations': '900'},
{'country': 'France', 'rank': '3', 'populations': '1400'}]
UPDATE:
Turns out the populations should be sorted so that the greatest population goes to the first country in the list, and the smallest population goes to the last country in the list, and the ranks are ranked by the number of population of each country:
countries = ['Argentina', 'Brazil', 'France']
population = ['100', '900', '1400']
ranks = ['1', '2', '3']
population = sorted(population,reverse=True,key=lambda x:int(x.replace(',','')))
ranks = sorted(ranks,key=lambda x:int(x))
result_list_of_dicts = [{'country': c,
'rank': r,
'populations': p}
for c, p, r in
zip(countries,
population,
ranks)]
print(result_list_of_dicts)
Output:
[{'country': 'Argentina', 'rank': '1', 'populations': '1400'},
{'country': 'Brazil', 'rank': '2', 'populations': '900'},
{'country': 'France', 'rank': '3', 'populations': '100'}]
The ranks
list can be left out altogether now that we know the ranks are consistent:
countries = ['Argentina', 'Brazil', 'France']
population = ['100', '900', '1400']
population = sorted(population,reverse=True,key=lambda x:int(x.replace(',','')))
result_list_of_dicts = [{'country': c,
'rank': r,
'populations': p}
for r, (c, p) in
enumerate(zip(countries,
population), 1)]
print(result_list_of_dicts)
Output:
[{'country': 'Argentina', 'rank': 1, 'populations': '1400'},
{'country': 'Brazil', 'rank': 2, 'populations': '900'},
{'country': 'France', 'rank': 3, 'populations': '100'}]