I'm a Python newbie and currently learning about dictionaries. I have the following exercise to make: Create a dict, country_counts, whose keys are country names, and whose values are the number of times the country occurs in the countries list.
the following is the code, which is working without a problem.
from countries import country_list # Note: the list is so large,
# and is extracted from another file
country_counts = {}
for country in country_list:
if country not in country_counts:
country_counts[country] = 1
else:
country_counts[country] = country_counts[country] + 1
I didn't understand how did the keys of country_list get inserted in country_count, with this for loop without assigning the keys? hope that makes sense. Thank you