close

[Solved] Python AttributeError: ‘dict’ object has no attribute ‘append’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python AttributeError: ‘dict’ object has no attribute ‘append’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python AttributeError: ‘dict’ object has no attribute ‘append’ Error Occurs?

Today I get the following error Python AttributeError: ‘dict’ object has no attribute ‘append’ in python.

How To Solve Python AttributeError: ‘dict’ object has no attribute ‘append’ Error ?

  1. How To Solve Python AttributeError: 'dict' object has no attribute 'append' Error ?

    To Solve Python AttributeError: 'dict' object has no attribute 'append' Error Like the error message suggests, dictionaries in Python do not provide an append operation.

  2. Python AttributeError: 'dict' object has no attribute 'append'

    To Solve Python AttributeError: 'dict' object has no attribute 'append' Error Like the error message suggests, dictionaries in Python do not provide an append operation.

Solution 1

Like the error message suggests, dictionaries in Python do not provide an append operation.

You can instead just assign new values to their respective keys in a dictionary.

mydict = {}
mydict['item'] = input_value

If you’re wanting to append values as they’re entered you could instead use a list.

mylist = []
mylist.append(input_value)

Your line user['areas'].append[temp] looks like it is attempting to access a dictionary at the value of key 'areas', if you instead use a list you should be able to perform an append operation.

Using a list instead:

user['areas'] = []

On that note, you might want to check out the possibility of using a defaultdict(list) for your problem.

Solution 2

Either use dict.setdefault() if the key is not added yet to dictionary :

dict.setdefault(key,[]).append(value)

or use, if you already have the keys set up:

dict[key].append(value)

source: stackoverflow answers

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read