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:
import csv path = #Path_to_CSV_File country_list=[] capital_list=[] country_dict={'Country':[],'Capital':[]} with open(path, mode='r') as data: for line in csv.DictReader(data): locals().update(line) country_dict['Country'].append(Country) country_dict['Capital'].append(Capital) country_list.append(Country) capital_list.append(Capital) i=14 #set pointer value to the 15th row in the csv document #---------------------- Iterating through Dictionary using for loops--------------------------- if i >= (len(country_dict['Country'])-1): print("out of bound") for count1, element in enumerate(country_dict['Country']): if count1==i: print('Country = ' + element) for count2, element in enumerate(country_dict['Capital']): if count2==i: print('Capital = ' + element) #--------------------------------Direct print for list---------------------------------------- print('Country = ' + country_list[i] + 'nCapital = ' + capital_list[i])
The output:
Country = Djibouti Capital = Djibouti (city) Country = Djibouti Capital = Djibouti (city)
The CSV file content:
Country,Capital Algeria,Algiers Angola,Luanda Benin,Porto-Novo Botswana,Gaborone Burkina Faso,Ouagadougou Burundi,Gitega Cabo Verde,Praia Cameroon,Yaounde Central African Republic,Bangui Chad,N'Djamena Comoros,Moroni "Congo, Democratic Republic of the",Kinshasa "Congo, Republic of the",Brazzaville Cote d'Ivoire,Yamoussoukro Djibouti,Djibouti (city) Egypt,Cairo Equatorial Guinea,"Malabo (de jure), Oyala (seat of government)" Eritrea,Asmara Eswatini (formerly Swaziland),"Mbabane (administrative), Lobamba (legislative, royal)" Ethiopia,Addis Ababa Gabon,Libreville Gambia,Banjul Ghana,Accra Guinea,Conakry Guinea-Bissau,Bissau Kenya,Nairobi Lesotho,Maseru Liberia,Monrovia Libya,Tripoli Madagascar,Antananarivo Malawi,Lilongwe Mali,Bamako Mauritania,Nouakchott Mauritius,Port Louis Morocco,Rabat Mozambique,Maputo Namibia,Windhoek Niger,Niamey Nigeria,Abuja Rwanda,Kigali Sao Tome and Principe,São Tomé Senegal,Dakar Seychelles,Victoria Sierra Leone,Freetown Somalia,Mogadishu South Africa,"Pretoria (administrative), Cape Town (legislative), Bloemfontein (judicial)" South Sudan,Juba Sudan,Khartoum Tanzania,Dodoma Togo,Lomé Tunisia,Tunis Uganda,Kampala Zambia,Lusaka Zimbabwe,Harare
Advertisement
Answer
I am not sure if I get your point; Please check out the code.
import csv path = #Path_to_CSV_File country_dict={} with open(path, mode='r') as data: lines = csv.DictReader(data) for idx,line in enumerate(lines): locals().update(line) country_dict[idx] = {"Country":Country,"Capital":} i=14 #set pointer value to the 15th row in the csv document #---------------------- Iterating through Dictionary using for loops--------------------------- country_info = country_dict.get(i) #--------------------------------Direct print for list---------------------------------------- print('Country = ' + country_info['Country'] + 'nCapital = ' + country_info['Capital'])