Descriptor 'append' for 'list' objects doesn't apply to a 'int' object

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

banner

Photo by Studio Ghaaf

Descriptor 'append' for 'list' objects doesn't apply to a 'int' object #

The Python "TypeError: descriptor 'append' for 'list' objects doesn't apply to a 'int' object" occurs when we call the append() method on the list class. To solve the error, call the method on an instance of the class instead, e.g. list().append(1).

typeerror descriptor append for list objects doesnt apply to a int

Here is an example of how the error occurs.

main.py
# ⛔️ TypeError: descriptor 'append' for 'list' objects doesn't apply to a 'int' object list.append(1)

We called the append method on the list class which caused the error.

We should instead call the append() method on a list object.

main.py
my_list = list() my_list.append(1) my_list.append(2) print(my_list) # 👉️ [1, 2]

We used parenthesis to instantiate the list class and called the append() method on an instance of the class.

Note that a much more common way to declare a variable that stores a list is to use square brackets.

main.py
my_list = [] print(type(my_list)) # 👉️ <class 'list'> my_list.append(1) my_list.append(2) print(my_list) # 👉️ [1, 2]

The example above achieves the same result.

The list.append() method adds an item to the end of the list.

main.py
my_list = [1, 2, 3] my_list.append(4) print(my_list) # 👉️ [1, 2, 3, 4]

The method returns None as it mutates the original list.

Make sure you aren't storing the result of calling append() in a variable because the method mutates the original list in place and returns None.

If you need to append multiple values to a list, use the extend() method instead.

The list.extend method takes an iterable (such as a list) and extends the list by appending all of the items from the iterable.

main.py
my_list = [1, 2] my_list.extend([3, 4, 5]) print(my_list) # 👉️ [1, 2, 3, 4, 5]

The list.extend method returns None as it mutates the original list.

You can print the list class and an instance of the class to see the difference between the two.

main.py
print(list) # 👉️ <class 'list'> print(list()) # 👉️ []

The first call to the print function prints the class, whereas the second prints an instance of the class.

You first have to instantiate the list class in order to use methods like append.

Use the search field on my Home Page to filter through my more than 1,000 articles.