-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvert_List_to_Dictionary.py
More file actions
39 lines (35 loc) · 1.1 KB
/
Convert_List_to_Dictionary.py
File metadata and controls
39 lines (35 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
zip_iterator = zip(list1, list2)
result_dict = dict(zip_iterator)
print(result_dict)
# {1: 'a', 2: 'b', 3: 'c'}
def list2dict(key_list, value_list):
"""Return a dictionary where key_list provides the keys and value_list provides the values."""
zipped_iterator = zip(key_list, value_list)
result_dict = dict(zipped_iterator)
return result_dict
list_of_list = [
['Arab World',
'ARB',
'Adolescent fertility rate (births per 1,000 women ages 15-19)',
'SP.ADO.TFRT',
'1960',
'133.56090740552298'],
['Arab World',
'ARB',
'International migrant stock, total',
'SM.POP.TOTL',
'1960',
'3324685.0']]
var_name = [
'CountryName',
'CountryCode',
'IndicatorName',
'IndicatorCode',
'Year',
'Value']
# Convert list of lists into list of dicts
list_of_dict = [list2dict(var_name, list_i) for list_i in list_of_list]
print(list_of_dict[0])
# {'CountryName': 'Arab World', 'CountryCode': 'ARB', 'IndicatorName': 'Adolescent fertility rate (births per 1,000 women ages 15-19)', 'IndicatorCode': 'SP.ADO.TFRT', 'Year': '1960', 'Value': '133.56090740552298'}