Python Dictionary Hurricanes Challenge Project: 'dict' object has no attribute 'append'

I’ve gotten totally hung up on question 4 of the Python Dictionary Hurricanes Challenge Project. The question asks you to write a function that converts the current dictionary of hurricanes to a new dictionary, where the keys are years and the values are lists containing a dictionary for each hurricane that occurred in that year.

I feel like my answer and the answer provided in Codecademy’s solution are fundamentally trying to do the same thing. But my solution throws the error "AttributeError: ‘dict’ object has no attribute ‘append.’ I’ve looked around the internet about this error, and I keep seeing different threads on whether appending a dictionary is even possible.

Does the dict() object have something to do with this? I feel like it was brushed over in the course material, but was used a few times in the provided solution for this challenge.

This is Codecademy’s solution.

def create_year_dictionary(hurricanes):
  hurricanes_by_year= dict()
  for cane in hurricanes:
      current_year = hurricanes[cane]['Year']
      current_cane = hurricanes[cane]
      if current_year not in hurricanes_by_year:
          hurricanes_by_year[current_year] = [current_cane]
      else:
          hurricanes_by_year[current_year].append(current_cane)
  return hurricanes_by_year

hurricanes_by_year = create_year_dictionary(hurricanes)
print(hurricanes_by_year[2005])

This is my solution that gets the error "AttributeError: ‘dict’ object has no attribute ‘append’

def create_year_dictionary():
  hurricanes_by_year = dict()
  for key, value in hurricanes.items():
    current_year = value["Year"]
    current_hurricane = key
    if current_year not in hurricanes_by_year:
      hurricanes_by_year[current_year] = value
    else:
      hurricanes_by_year[current_year].append(current_hurricane)
  return hurricanes_by_year

print(create_year_dictionary())

Any help would be appreciated, thank you!

Hello, @corynbriere854971788, and welcome to the forums. I’d take Python at its word. There is no dict.append() method. There is, however, a way to add key/value pairs to a dictionary. It’s used in the Codecademy solution you posted as well as in your own solution. There is a very important difference between the two code snippets. The CC solution code uses append(), but what is the type of the object the append() method is called on? (It’s not a dictionary.)

3 Likes

Thank you for your quick reply. I’m not sure I follow. Doesn’t

hurricanes_by_year[current_year].append(current_hurricane)

append

hurricanes_by_year = dict()

In both cases?

For the life of me I can’t figure out how to add multiple values as dictionaries to a single key, and I double checked course material and it never showed us how to append a key, value pair without overwriting the value. Which as you said, shouldn’t be possible?

I also found an error in my code, so I will update that here.

def create_year_dictionary():
  hurricanes_by_year = dict()
  for key, value in hurricanes.items():
    current_year = hurricanes[key]['Year']
    current_cane = hurricanes[key]
    if current_year not in hurricanes_by_year:
      hurricanes_by_year[current_year] = value
    else:
      hurricanes_by_year[current_year].append(current_cane)
  return hurricanes_by_year

The difference between the solution:

and your code:

What’s the significance of the []'s?

Hint:

The purpose of the if...else is to determine whether a year is already in the dictionary. If it isn’t, it is added as a new key, and the hurricane is added as the value. If the year is already in the dictionary, the current hurricane is appended to what in the solution?

Additional Hint:

The solution creates a list that includes the hurricane dictionary object as the value for a new key (year). Then additional hurricanes for that year are appended to the list.

2 Likes

Hey @midlindner, Hey @corynbriere854971788

Thank you so much for this topic! :smiley_cat: :smiley_cat: :smiley_cat:
I have been stuck for two days on this struggling to figure out the solution, but thanks to you I am now pretty close to comprehending this in 100% :joy:

2 Likes

Thank you so much! That fixed it. If I understand correctly, using brackets around [value] when adding the key, value pair to a dictionary will initialize all values as lists. That list can contain one or multiple elements. While dictionaries themselves cannot be appended, a value that is initialized as list can be appended to contain more elements. In this case, it allows us to add multiple hurricane values as elements to a single ‘Year’ key.

1 Like

Yes you’re right, if you’re not familiar with the syntax it’s called a list literal. Whilst the square brackets are used for subscripts/indices when directly follow an identifier name[subscirpt] when used without the preceding name it does indeed create a list with the elements between the [] and separated by commas, for example-

lst = [1, 2, 3]

There’s nothing special about doing this with a dictionary. It creates a list and you simply create a dictionary key that refers to this list.

lst = [1, 2, 3]  # create list
newdict = dict()  # create dict
newdict["key"] = lst  # create a key referring to list
print(newdict)

# key refers to list so we use the *list* method .append
newdict["key"].append(4)  
# change can now be seen
print(newdict)
# we have more than one reference to the list
print(lst)
# it is the list object itself which has changed
lst.append(5)
print(newdict)  # same object

Since it’s looped you just create a lot of lists and create dictionary keys that refer to them. I hope that’s clearer (and not more confusing :joy:).

3 Likes

Thank you for the succinct example! I would not have guessed that appending a list via newdict["key"].append(4) would also update lst.

1 Like

If you can wrap your head around it then you’re starting starting to understand the way Python handles objects which is good. If you’ve got the time it’s worth a quick nosey into object oriented programming. It’s a slightly odd concept at first that may take some effort but once you get it a lot of puzzle pieces fall into place. Something to keep in mind anyway.