I have been practicing on iterating through dictionary and list in Python.
The source file is a csv document containing Country and Capital. It seems I had to go through 2 for loops for country_dict in order to produce the same print result for country_list and capital_list.
Is there a better way to do this in Python dictionary?
The code:
JavaScript
x
30
30
1
import csv
2
path = #Path_to_CSV_File
3
country_list=[]
4
capital_list=[]
5
country_dict={'Country':[],'Capital':[]}
6
with open(path, mode='r') as data:
7
for line in csv.DictReader(data):
8
locals().update(line)
9
country_dict['Country'].append(Country)
10
country_dict['Capital'].append(Capital)
11
country_list.append(Country)
12
capital_list.append(Capital)
13
14
i=14 #set pointer value to the 15th row in the csv document
15
16
#---------------------- Iterating through Dictionary using for loops---------------------------
17
if i >= (len(country_dict['Country'])-1):
18
print("out of bound")
19
20
for count1, element in enumerate(country_dict['Country']):
21
if count1==i:
22
print('Country = ' + element)
23
24
for count2, element in enumerate(country_dict['Capital']):
25
if count2==i:
26
print('Capital = ' + element)
27
#--------------------------------Direct print for list----------------------------------------
28
29
print('Country = ' + country_list[i] + 'nCapital = ' + capital_list[i])
30
The output:
JavaScript
1
5
1
Country = Djibouti
2
Capital = Djibouti (city)
3
Country = Djibouti
4
Capital = Djibouti (city)
5
The CSV file content:
JavaScript
1
56
56
1
Country,Capital
2
Algeria,Algiers
3
Angola,Luanda
4
Benin,Porto-Novo
5
Botswana,Gaborone
6
Burkina Faso,Ouagadougou
7
Burundi,Gitega
8
Cabo Verde,Praia
9
Cameroon,Yaounde
10
Central African Republic,Bangui
11
Chad,N'Djamena
12
Comoros,Moroni
13
"Congo, Democratic Republic of the",Kinshasa
14
"Congo, Republic of the",Brazzaville
15
Cote d'Ivoire,Yamoussoukro
16
Djibouti,Djibouti (city)
17
Egypt,Cairo
18
Equatorial Guinea,"Malabo (de jure), Oyala (seat of government)"
19
Eritrea,Asmara
20
Eswatini (formerly Swaziland),"Mbabane (administrative), Lobamba (legislative, royal)"
21
Ethiopia,Addis Ababa
22
Gabon,Libreville
23
Gambia,Banjul
24
Ghana,Accra
25
Guinea,Conakry
26
Guinea-Bissau,Bissau
27
Kenya,Nairobi
28
Lesotho,Maseru
29
Liberia,Monrovia
30
Libya,Tripoli
31
Madagascar,Antananarivo
32
Malawi,Lilongwe
33
Mali,Bamako
34
Mauritania,Nouakchott
35
Mauritius,Port Louis
36
Morocco,Rabat
37
Mozambique,Maputo
38
Namibia,Windhoek
39
Niger,Niamey
40
Nigeria,Abuja
41
Rwanda,Kigali
42
Sao Tome and Principe,São Tomé
43
Senegal,Dakar
44
Seychelles,Victoria
45
Sierra Leone,Freetown
46
Somalia,Mogadishu
47
South Africa,"Pretoria (administrative), Cape Town (legislative), Bloemfontein (judicial)"
48
South Sudan,Juba
49
Sudan,Khartoum
50
Tanzania,Dodoma
51
Togo,Lomé
52
Tunisia,Tunis
53
Uganda,Kampala
54
Zambia,Lusaka
55
Zimbabwe,Harare
56
Advertisement
Answer
I am not sure if I get your point; Please check out the code.
JavaScript
1
18
18
1
import csv
2
path = #Path_to_CSV_File
3
country_dict={}
4
with open(path, mode='r') as data:
5
lines = csv.DictReader(data)
6
for idx,line in enumerate(lines):
7
locals().update(line)
8
country_dict[idx] = {"Country":Country,"Capital":}
9
10
i=14 #set pointer value to the 15th row in the csv document
11
12
#---------------------- Iterating through Dictionary using for loops---------------------------
13
country_info = country_dict.get(i)
14
#--------------------------------Direct print for list----------------------------------------
15
16
print('Country = ' + country_info['Country'] + 'nCapital = ' + country_info['Capital'])
17
18