How to Fix AttributeError: ‘list’ object has no attribute ‘update’

AttributeError: ‘list’ object has no attribute ‘update’ error occurs when you try to operate the update() method on the list object, which does not exist.

Reproduce the error

numbers = [1, 2, 3]

numbers.update(4)

print(numbers)

Output

Why the AttributeError - 'list' object has no attribute 'update' error occurs

How to fix the error?

Here are three ways to fix the error.

  1. Using the list.append()
  2. Using the list.extend()
  3. Using the list.insert()

Diagram of fixing the AttributeError - 'list' object has no attribute 'update'

Solution 1: Using the list.append()

You can use the list append() method to add elements to a list.

numbers = [1, 2, 3]
numbers.append(4)

print(numbers)

Output

list.append() method

Solution 2: Using the list.extend()

The list.extend() method extends the list by appending all elements from an iterable-like list.

numbers = [1, 2, 3]
digits = [11, 21, 31]

numbers.extend(digits)
print(numbers)

Output

Using the list.extend()

Solution 3: Using the list.insert()

The list.insert() method will help you add an item at a specific index rather than at the end.

Using the list.insert()

I hope you will use one of these three methods instead of the update() method to prevent the error.

Related posts

AttributeError: ‘list’ object has no attribute ‘copy’

AttributeError: ‘list’ object has no attribute ‘str’

AttributeError: ‘list’ object has no attribute ‘split’

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.