Skip to content
Advertisement

Why is my attempt to iterate through a list of lists and update dictionary values resulting in unchanged dictionary?

I want to iterate through a list of lists and if an instance in the list matches a key in a dictionary, add 1 to that dictionary key value. However, when I execute my code it returns the dictionary with no changes to the values. Thank you for your time & help. I searched for a similar question but did not find one that addressed this issue. My apologies if I missed it.

I have a list of hurricanes and the areas they affected. I have a dictionary with key, value pairs for the uniquely affected area, and the number of times affected (set to zero). I want to iterate through the list of hurricane instances and each time I pass through a location in the dictionary increase the value of the unique area by 1. Currently, with the code I’m running the returned dictionary is unchanged.

I’ve included smaller examples of material I’m working with: (test_list is a list of areas affected by a hurricane resulting in a list of lists)

test_list = [['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], ['The Bahamas', 'Northeastern United States'], ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda']]

test_dict = {
'Central America': 0, 'Mexico': 0, 'Cuba': 0, 'Florida', 0, 'The Bahamas': 0, 'Lesser Antilles': 0, 'United States East Coast', 'Atlantic Canada': 0, 'Northeastern United States': 0, 'Jamaica': 0, 'Cayman Islands': 0, 'Bermuda': 0}

GOAL

if I performed:

area_counted = area_counter(test_list, test_dict)

I would have expected:

area_counted = {
'Central America': 1, 'Mexico': 1, 'Cuba': 2, 'Florida', 1, 'The Bahamas': 4, 'Lesser Antilles': 2, 'United States East Coast': 1, 'Atlantic Canada': 1, 'Northeastern United States': 1, 'Jamaica': 1, 'Cayman Islands': 1, 'Bermuda': 1}

However, this was not my result.

CODE

def area_count(input_dict, input_list):
  new_dict = {}
  new_dict.update(input_dict)
  for i in list:
      for j in i:
        if j == new_dict[j]:
          new_dict[j] += 1
        else:
          pass
  return new_dict

area_counted = area_count(test_dict, test_list)
print(area_counted)

outputs the following:

area_counted = {
'Central America': 0, 'Mexico': 0, 'Cuba': 0, 'Florida', 0, 'The Bahamas': 0, 'Lesser Antilles': 0, 'United States East Coast', 'Atlantic Canada': 0, 'Northeastern United States': 0, 'Jamaica': 0, 'Cayman Islands': 0, 'Bermuda': 0}

edit-1: edited area_count parameters to input_list instead of list, corrected parameter input order for area_counted to match area_count function.

Advertisement

Answer

new_dict[j] always is equal to 0 initially (since in test_dict all the values are 0)

you are comparing it to a string

if "Central America" == 0:
    set_to_1()

hopefully you can see how this never sets it to 1

I think what you want is

if j in new_dict:
    new_dict[j] = 1

as an unrelated aside you really should not name a variable list as that is a built in entity that you end up shadowing

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement