Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionaries
#1
I need help with looping through the values of a dictionary.
def combine_dict(year_dict, headers):
    borough={}
    i=0
    for x in year_dict:
        while i<len(headers)-1:
            borough[headers[i+1]]=year_dict[x][i]
            i+=1
    for y in nums:
        nums[y]=borough
    return(nums)  

pop_dict= combine_dict(pop_dict, pop_headers)
pprint(pop_dict)



The output only takes the first row of the dictionary instead of the entire dictionary. For example, Year 1800 should have different values for each borough. What's wrong with my code?
Output:
{'1790': {'Bronx': '1781', 'Brooklyn': '4549', 'Manhattan': '33131', 'Queens': '6159', 'Staten Island': '3827'}, '1800': {'Bronx': '1781', 'Brooklyn': '4549', 'Manhattan': '33131', 'Queens': '6159', 'Staten Island': '3827'},
buran write May-21-2021, 04:50 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Messages In This Thread
Dictionaries - by ChloeH - May-21-2021, 03:03 AM
RE: Dictionaries - by perfringo - May-21-2021, 06:32 AM
RE: Dictionaries - by Larz60+ - May-21-2021, 11:25 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020