According to Python wiki: Time complexity, set is implemented as a hash table. So you can expect to lookup/insert/delete in O(1) average. Unless your hash table's load factor is too high, then you face collisions and O(n).

P.S. for some reason they claim O(n) for delete operation which looks like a mistype.

P.P.S. This is true for CPython, pypy is a different story.

Answer from Sergey Romanovsky on Stack Overflow
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
What's the time complexity of set.add()? - Python Help - Discussions on Python.org
March 27, 2024 - when I was reading the python docs at this part click here I had a slight doubt that whenever we perform any set.add(item) does this happens to traverse the entire set for item equality with the elements already inside the set. Code: class Foo: def __eq__(self, other): print("Called me.") return id(self) == id(other) def __hash__(self): return 1 def __repr__(self): return "Dummy()" s = {Foo(), Foo(), Foo(), Foo()} print("==========") s.add(Foo...
Discussions

Time complexity of python set operations?
They are implemented using hash ... operations on sets have efficient time complexities. Here is an overview of the time complexities for various operations on Python sets: ... Adding an element to a set is generally an O(1) operation.... More on designgurus.io
๐ŸŒ designgurus.io
1
10
June 21, 2024
python - What is time complexity of a list to set conversion? - Stack Overflow
I've noticed the table of the time complexity of set operations on the python official website. But i just wanna ask what's the time complexity of converting a list to a set, for instance, l = [1,... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Time complexity of sets
Ok, so sets/dictionaries work by hashing the index value. So that it's a constant time to find the item. You don't iterate through the set/dictionary. You just simply ask what is the value at this address? Let's say there are a number of people living on a street, everyone lives at the address that matches the length of their last name, and I told you got go to "smith" You wouldn't spend time checking houses to find smith, you would immediately go to house 5. The constant time spent was converting smith to 5. It would take you the same constant time to find where Scot or Johnson lived. That's how a hash works, it converts whatever value you have into an address in memory. It gets a bit more complex than just "length" and there is code in place to handle collisions (smith and jones are not at the same address). But that's the simple version of it. I understand that sets are data structures where all its elements are sorted They're not sorted. They're unordered. In recent version of python dictionaries maintain "insertion order". More on reddit.com
๐ŸŒ r/learnpython
4
1
March 17, 2021
Time complexity for adding elements to list vs set in python - Stack Overflow
Why does adding elements to a set ... list in python? I created a loop and iterated over 1000000 elements added it to a list and a set. List is consistently taking around 10 seconds vs set which is taking around 20 seconds. ... Save this answer. ... Show activity on this post. Both operations are O(1) amortized time-complexity... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ time-complexity-for-adding-element-in-python-set-vs-list
Time Complexity for Adding Element in Python Set vs List - GeeksforGeeks
July 23, 2025 - When we add an element to a list using the append() method, Python directly adds the element to the end. This operation has O(1) amortized time complexity, as no hashing or duplicate checks are needed.
๐ŸŒ
Code Like A Girl
code.likeagirl.io โ€บ time-complexities-of-python-dictionary-and-set-operations-ee13511a2881
Time Complexities of Python Dictionary and Set Operations | by Python Code Nemesis | Code Like A Girl
November 7, 2023 - In most practical scenarios, the time complexity of inserting an element into a Python set is O(1) on average, with the caveat that it can be O(n) in the worst case due to hash collisions.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python set add()
Python Set add() โ€“ Be on the Right Side of Change
November 2, 2022 - The runtime complexity of the set.add() ... to time you may run into collisions which could cause the runtime complexity to increase to O(n) due to the collision handling....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ internal-working-of-set-in-python
Internal working of Set in Python - GeeksforGeeks
May 10, 2025 - The clear() method removes all elements from the set, effectively making it an empty set. This operation has a time complexity of O(n). ... If Multiple values are present at the same index position, then the value is appended to that index position, to form a Linked List. In, Python Sets are implemented using dictionary with dummy variables, where key beings the members set with greater optimizations to the time complexity.
๐ŸŒ
Quora
quora.com โ€บ Why-do-sets-in-Python-have-an-algorithmic-complexity-of-O-1
Why do sets in Python have an algorithmic complexity of O(1)? - Quora
Answer (1 of 6): A hash table has expected time complexity for insertion, deletion, and membership checking that is constant in the number of entries being stored. Pythonโ€™s set is built on a hash table implementation. But this conceals some assumptions which can be violated in practice. The con...
Find elsewhere
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ TimeComplexity
TimeComplexity - Python Wiki
Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still. See dict -- the implementation is intentionally very similar. As seen in the source code the complexities for set difference s-t or s.difference(t) (set_difference()) and in-place set difference s.difference_update(t) (set_difference_update_internal()) are different!
๐ŸŒ
Medium
medium.com โ€บ @mohitarvindjoshi โ€บ the-truth-about-python-sets-nobody-told-you-ada65e00cfe3
The Truth About Python Sets Nobody Told You | by Mohit Joshi | Medium
June 28, 2025 - So, the average case remains O(1), but in the worst case (many collisions), it can go up to O(n). Again, such cases are rare due to Pythonโ€™s hash function design and resizing strategy.
๐ŸŒ
YouTube
youtube.com โ€บ tech with tim
Python Sets Tutorial #1 & Time Complexity (BIG O) - YouTube
In this video I explain how to implement sets in python and explain the main advantages and disadvantages of them. I go over creating sets, removing and addi...
Published: November 17, 2018
Views: 3K
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ time complexity of sets
r/learnpython on Reddit: Time complexity of sets
March 17, 2021 -

I understand that sets are data structures where all its elements are sorted and it doesn't contain any duplicate values, but why is their time complexity just O(1)?

How can it be a constant value, even if the set contains millions of elements?

I thought that the complexity was O(n*log(n)) due to a binary search, but looks like it's even faster and I can't really understand how.

Thanks in advance for any answer!

Top answer
1 of 3
2
Ok, so sets/dictionaries work by hashing the index value. So that it's a constant time to find the item. You don't iterate through the set/dictionary. You just simply ask what is the value at this address? Let's say there are a number of people living on a street, everyone lives at the address that matches the length of their last name, and I told you got go to "smith" You wouldn't spend time checking houses to find smith, you would immediately go to house 5. The constant time spent was converting smith to 5. It would take you the same constant time to find where Scot or Johnson lived. That's how a hash works, it converts whatever value you have into an address in memory. It gets a bit more complex than just "length" and there is code in place to handle collisions (smith and jones are not at the same address). But that's the simple version of it. I understand that sets are data structures where all its elements are sorted They're not sorted. They're unordered. In recent version of python dictionaries maintain "insertion order".
2 of 3
2
As others have pointed out, these are implemented with hash tables. Hashing is when you generate some pseudorandom number from some input data. In a hash table, that number is clipped (modulo) so as to fit inside the table. Ideally, different data will always get you a different number so you end up in the right spot of the hash table in constant time, but that's obviously not always going to happen and you will get so-called hash collisions. When those happen, some sort of strategy is necessary to deal with them and since you'd ideally design your hash table so they don't happen very often, that strategy tends to just be to use the next spot in the table, and then just linearly search. In that sense, it's not exactly a constant-time algorithm, but you really should only be searching a very small potion of the full table, so it's close. As the table fills up (its "load factor" increases), this cost generally grows, although that is not universally true (e.g., when perfect hashing is an option). It can also happen that the hash table needs to be grown, which will generally not be a constant-time operation. There are all sorts of strategies for that. More often than not, though, the hashing step will not lead to a collision, and you get O(1) performance.
Top answer
1 of 2
13

Both operations are O(1) amortized time-complexity.

Appending elements to a list has a lower coefficient because it does not need to hash the element first, nor does it need to check/handle hash collisions.

In the case of adding x into a set, Python needs to compute hash(x) first, because keeping the hash of all elements is what allows sets to have fast O(1) membership checks (compared to O(n) membership checks for lists).

2 of 2
8

The time complexity for appending to a list is the same as for adding to a set - both are O(1) amortised operations, meaning on average they each take a constant amount of time, although occasionally the operation may take more than that constant amount of time in order to dynamically resize the array that the data is stored in.

However, just because both are O(1) doesn't mean they take the same amount of time:

  • Appending to a list should be faster, because a list is implemented as a dynamic array, so appending an element just requires writing that element at the right index (which is already known), and increasing the length by 1.
  • In contrast, adding to a set is slower, because it requires computing the hash of the element to find the index to start looking from, then testing indices in some sequence to see if the element is already there (by testing if there is an element at the index, and if so, whether it equals the element being inserted) until either finding it, or finding an empty space where the element should be added.
๐ŸŒ
Medium
binarybeats.medium.com โ€บ python-set-data-structure-methods-use-time-and-space-complexity-366b8c408345
Python Set Data Structure: Methods, Use, Time, and Space Complexity | by Binary Beats | Medium
April 22, 2023 - By using sets, we can solve this problem efficiently in O(n) time complexity, where n is the total number of elements in both lists.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python dict vs set operations time complexities
r/learnpython on Reddit: Python dict vs set operations time complexities
September 7, 2022 -

Hello! I am trying to get down the big O for a few set operations, but I'm a bit confused. I know dicts are hash maps that have O(1) insertion, lookup, and removal times. I know set() are supposed to be similar with constant insertion, lookup, and removal as well (with the addition of no duplications + you cant lookup by index).

However, from my understanding sets are sorted, so wouldnt that make things slower, like O(logn) slower? I havent been able to find an answer that addresses the sorted feature of sets, so the help would be much appreciated. Thank you!

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ time-complexity-of-a-list-to-set-conversion-in-python
Time Complexity of A List to Set Conversion in Python - GeeksforGeeks
July 23, 2025 - The average-case time complexity is O(n), where n is the number of elements in the list. In this article, we will see compare different scenarios in which we will compare time complexities for different conversion from a list to a set in Python.
๐ŸŒ
GitHub
gist.github.com โ€บ Gr1N โ€บ 60b346b5e91babb5efac
Complexity of Python Operations ยท GitHub
Complexity of Python Operations. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ sets-python
Sets In Python - Flexiple
March 15, 2024 - Python sets are implemented using hash tables, which allows for fast operations on average. The key operations and their average time complexities are as follows: Adding an Element (Add): The add() method has an average time complexity of O(1).