The python AttributeError: ‘dict’ object has no attribute ‘append’ error happens when the append() attribute is called in the dict object. The dict object does not support the attribute append(). The elements can be added by assignment operator in dict. If the append() function is called in the ‘dict’, the error AttributeError: ‘dict’ object has no attribute ‘append’ will be thrown.

The python AttributeError: ‘dict’ object has no attribute ‘append’ error occurs when you try to append a value in a dict object. The dict should be modified as list or the values should be added as key value in the dict object. Before calling the append method, the object type should be verified.

The python dict contains a key value pair element. You can store or retrieve values using the key. The value can be accessed as a python list. The dict does not support attributes such as the append (). The python dict object is used for values in the key value pair and the values can be accessed using the key.



Exception

The python AttributeError: ‘dict’ object has no attribute ‘append’ error will be seen as below the stack trace. The python interpreter can not append a value in dict. The error will be seen as follows.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    a.append(' World')
AttributeError: 'dict' object has no attribute 'append'
[Finished in 0.0s with exit code 1]


How to reproduce this error

The error AttributeError: ‘dict’ object has no attribute ‘append’ can be reproduced in a dict object. If you call the append() function in a dict object, an error will occur. Create a dict object and store it in a variable. call the append function with an argument. The dict object did not locate the append() function. Therefore, the error AttributeError: ‘dict’ object has no attribute ‘append’ will be thrown.

Program

a = {};
a.append(' World')
print a

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    a.append(' World')
AttributeError: 'dict' object has no attribute 'append'
[Finished in 0.0s with exit code 1]


Root Cause

The python class is a collection of data and functionality. The object in python is an enclosed collection of data and functionality identified as a class variable. The attribute in python is the collection of class-related data and functionality. These attributes are available for all class objects. The Attribute error is thrown if the attribute append is not supported by dict object.

The attributes of a class object will be accessed using the dot operator. if a python variable uses dot operator, the variable must be assigned with an object and the attribute must be supported by the object class. If the variable is assigned with a python dict object and the attribute append is called, the attribute error AttributeError: ‘dict’ object has no attribute ‘append’ will be thrown.



Solution 1

If the python variable is assigned to the dict object instead of the python list, the error AttributeError: ‘dict’ object has no attribute ‘append’ would occur. The python variable should be assigned to the python list instead of the python dict object. The example below illustrates how to create a python list to use the append function.

Program

a = [];
a.append(' World')
print a

Output

[' World']
[Finished in 0.0s]


Solution 2

If the python variable is assigned with a dict object and if you call the append function, the error AttributeError: ‘dict’ object has no attribute ‘append’ will occur. The python dict object should be assigned with a value using assignment operator as a key value pair. The example below illustrates how to assign a key value pair in a dict object.

Program

a = {};
a['key']= ' World'
print a

Output

{'key': ' World'}
[Finished in 0.1s]


Solution 3

If you want to use the append function in a dict object, the dict object should have a list as a key value. The key is added to the dict object with a value of the type list. The following example would demonstrate how to create a key with a value of the type list and how to use append function in a dict object.

Program

a = {};
a['key']= []
a['key'].append('val')
print a

Output

{{'key': ['val']}
[Finished in 0.0s]


Solution 4

The python variable should be checked for the required type or not before the append attribute is invoked. A list supports the append attribute. The python object must be verified as a list. If the python object is not a list, it should not be called the append attribute.

In the example below, the type of the python variable is verified as a list. If the variable type is list, the append attribute will be invoked. If the type of the python variable is not a list, the append function will not be invoked. the append code will be ignored for the execution. Hence, the error AttributeError: ‘dict’ object has no attribute ‘append’ will not occur.

Program

a = {};
if type(a) is list:
	a.append(' World')
print a

Output

{}
[Finished in 0.0s]


Solution 5

If the data type of the variable is unknown, the attribute will be invoked with try and except block. The try block will execute if the python variable contains list object. Otherwise, the except block will handle the error.

In the example below, the append() function is added with in the try block. if the python variable is assigned with a dict object, the error will be thrown. the error will be handled with except block. The except block code will be executed and printed the value in the console. If the python variable contains list, the append method will be executed and the except block will be ignored.

Program

a = {};
try :
	a.append(' World')
except :
	print 'error';
print a

Output

error
Hello
[Finished in 0.0s]



Leave a Reply