AttributeError: ‘Dict’ Object Has No Attribute ‘Append’ In Python

AttributeError: ‘dict’ object has no attribute ‘append’

You don’t know how to fix the “AttributeError: ‘dict’ object has no attribute ‘append'” in Python. Let’s follow the article to find the simple solution for you.

Why does the error “AttributeError: ‘dict’ object has no attribute ‘append'” occur?

The AttributeError: ‘dict’ object has no attribute ‘append’ occurs when we use the append() method on a dict because dict is a data structure and differs from the list. So, it is not an append() method in a data structure. Follow the code below to better understand.

Code example:

# Initialize a variable with dict type
sampledict = {}
sampledict["learn share IT"] = [1, 2, 3]

# Add to dict
sampledict.append(4)

print(sampledict)

Output:

AttributeError: 'dict' object has no attribute 'append'

Or another example:

Code example:

sampledict = {"lean share": "IT", "IT": "learn share"}

# Add key and value to dict
sampledict.append("share", "learn IT")

Output:

AttributeError: 'dict' object has no attribute 'append'

Next, I will share with you the most simple solution. Let’s continue reading it right below.

Solutions to fix this error

Use ‘list’ in a ‘dict’

Here, we can use the append() method to list in dict.

First, we need to create a dict, and then we can create an list in a dict, so we can use the ‘method’ without getting this error. See the code example below.

Code example:

# Initialize a variable dict
sampledict = {}

# Create a 'list' in a dict
sampledict["learn share IT"] = [1, 2, 3]

# Add 4 to the 'list' by the 'append' method
sampledict["learn share IT"].append(4)

print(sampledict)

Output:

{'learn share IT': [1, 2, 3, 4]}

Use the Dictionary update() method

In this solution, we will use the update() method to append dict.

Syntax:

sampledict.update(ite)

Parameters:

  • ite : a dict stores key and value.

First, we need to create a variable sampledict to store dict. Then, we need two variables key and value. If you want to add key and value to sampledict, we use update() method to append.

Code example:

# Initialize a variable sampledict store a dict
sampledict = {"learn": "share IT"}

# Initialize key and value you want to add
key = "share"
value = "learn IT"

# Add key and value to dict and print
sampledict.update({key: value})

print(sampledict)

Output:

{'learn': 'share IT', 'share': 'learn IT'}

Summary

Please read this article if you encounter “AttributeError: ‘dict’ object has no attribute ‘append'” error in Python. If you have any questions about this issue, please leave a comment below. I will answer your questions.

Have a good day!

Maybe you are interested in similar errors:

AttributeError: ‘dict’ object has no attribute ‘has_key’ in Python
AttributeError: ‘list’ object has no attribute ‘items’ in Python
AttributeError: ‘list’ object has no attribute ‘split’
AttributeError: ‘str’ object has no attribute in Python
AttributeError: ‘tuple’ object has no attribute in Python

Leave a Reply

Your email address will not be published. Required fields are marked *