Skip to content Skip to sidebar Skip to footer

Python Add Dictionary To Existing Dictionary - Attributeerror: 'dict' Object Has No Attribute 'append'

I'm trying to append an existing JSON file. When I overwrite the entire JSON file then everything works perfect. The problem that I have been unable to resolve is in the append. I'

Solution 1:

Usually python errors are pretty self-explanatory, and this is a perfect example. Dictionaries in Python do not have an append method. There are two ways of adding to dictionaries, either by nameing a new key, value pair or passing an iterable with key, value pairs to dictionary.update(). In your code you could do:

data['hashlist'][new_hash_val] = {'description': description, 'url': new_url_val}

or:

data['hashlist'].update({new_hash_val: {'description': description, 'url': new_url_val}})

The first one is probably superior for what you are trying to do, because the second one is more for when you are trying to add lots of key, value pairs.

You can read more about dictionaries in Python here.

Post a Comment for "Python Add Dictionary To Existing Dictionary - Attributeerror: 'dict' Object Has No Attribute 'append'"