AttributeError: 'list' object has no attribute 'add'
Codemia Knowledge Hub ·
add() is a method of set, not list, so calling it on a list raises AttributeError: 'list' object has no attribute 'add'. Use append() to add one item to a list, or switch to a set if what you really want is a collection of unique values.
Why lists have no add
A list is an ordered sequence that allows duplicates. A set is an unordered collection of unique items. The two have different semantics, so their method names differ.
This code fails:
list does not define add. Its API offers append, extend, insert and other sequence operations instead.
Use append or extend for lists
To add one element to the end of a list, use append:
To add several values from another iterable, use extend:
append adds one object, while extend iterates over the collection and adds each element separately. Mixing them up is easy:
This produces a nested list, because the whole inner list is appended as one element.
Use a set when uniqueness is the goal
The error can be a hint that the wrong container was chosen. If the code wants unique elements and an add operation, a set may be the right type:
The second add creates no duplicate, because sets enforce uniqueness.
- Use a
listwhen order matters or duplicates are meaningful. - Use a
setwhen uniqueness and fast membership tests matter more than order.
Swapping .add() for .append() without checking whether uniqueness was required can be the wrong fix: duplicates will start to appear.
Check the variable's type at runtime
In larger programs, the error often means a variable changed type earlier and later code still assumes the old type. Check what it actually is:
If items was meant to be a set but became a list through reassignment, the real bug is upstream, and fixing only the .add() line hides it. The same applies if you carried over habits from another language where list-like containers have an add method: Python lists do not.
Related reading
- Auto-initializing C lists
- Autocomplete using a trie
- Average values of dictionaries
- awaitable Task based queue
- AttributeError 'Model' object has no attribute '_backward_hooks
- AttributeError module '_Box2D' has no attribute 'RAND_LIMIT_swigconstant
- AttributeError module '_pywrap_tensorflow_internal' has no attribute 'TFE_DEVICE_PLACEMENT_EXPLICIT_swigconstant
- AttributeError module 'keras.utils' has no attribute 'Sequence
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.