๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ TimeComplexity
TimeComplexity - Python Wiki
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! The first one is O(len(s)) (for every element in s add it to the new set, if not in t).
Discussions

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
What's the time complexity of set.add()?
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 ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
0
March 27, 2024
python - What is time complexity of a list to set conversion? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I've noticed the table of the time complexity of set operations on the python official website. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python dict vs set operations time complexities
from my understanding sets are sorted No, sets are inherently unordered data structures. Their iteration order in practice is essentially random (though persistently so throughout the lifetime of a Python process). More on reddit.com
๐ŸŒ r/learnpython
11
1
September 7, 2022
๐ŸŒ
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 time to convert small list to set : 0.0 The set is : {1, 2, 3, 4, 5} The time to convert large list to set : 0.21737 ยท The time complexity of list to set conversion is O(n) where n is the number of element in the list.
๐ŸŒ
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.
๐ŸŒ
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 - O(1) for initializing a set is constant time and adding an elements. O(n) for printing the list, as it requires iterating through all elements. When we add an element to a list using the append() method, Python directly adds the element to the end.
๐ŸŒ
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 - Counting distinct elements โ€” You may need to count the number of distinct elements in a list or array. Using a set to store the elements and then getting the size of the set can quickly give you the count. The time and space complexity of set methods can vary depending on the operation being performed.
Find elsewhere
๐ŸŒ
DEV Community
dev.to โ€บ iihsan โ€บ time-complexity-analysis-of-python-methods-bigo-notations-for-list-tuple-set-and-dictionary-methods-47l9
Time Complexity Analysis of Python Methods: Big(O) Notations for List, Tuple, Set, and Dictionary Methods - DEV Community
January 15, 2024 - Whether you're working on real-world software or tackling problems in interviews, it's not just about writing code; it's all about writing efficient and scalable code. So, understanding the time complexity of your code becomes essential. In this article, we'll break down the Python methods for lists, tuples, sets, and dictionaries.
๐ŸŒ
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
That is, a 10x increase in key size led to a 10x increase in the time taken to do set membership. ... ), because it requires a comparison. But sometimes we just ignore this or say that the membership check is O(k) ... ) expected time complexity ...
๐ŸŒ
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...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ internal-working-of-set-in-python
Internal working of Set in Python - GeeksforGeeks
May 10, 2025 - The difference between two sets can be found using the difference() method or the - operator. The time complexity for this operation is O(len(s1)), where s1 is the set from which elements are being subtracted.
๐ŸŒ
UCI
ics.uci.edu โ€บ ~pattis โ€บ ICS-33 โ€บ lectures โ€บ complexitypython.txt
Complexity of Python Operations
------ 3) Algorithm 3: A list is ... O(1): 2 len (each O(1)) and == ints O(1) The complexity class for executing the entire function is O(N) + O(1) = O(N + 1) = O(N). So the complexity class for this algorithm/function is lower than both the first and second algorithms/...
๐ŸŒ
GitHub
gist.github.com โ€บ Gr1N โ€บ 60b346b5e91babb5efac
Complexity of Python Operations ยท GitHub
Complexity of Python Operations. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
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 - The time complexity of inserting an element into a Python set is typically O(1). This means that the time taken to insert an element into a set does not depend on the size of the set. The O1) time complexity for insertion is achievable because sets in Python are implemented using hash tables, ...
๐ŸŒ
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!

๐ŸŒ
CopyProgramming
copyprogramming.com โ€บ howto โ€บ time-complexity-of-python-set-operations
Python Time Complexity: Special Methods and Set Operations - Python time complexity of python special methods
August 11, 2022 - Python set add() is amortized O(1) average, O(n) worst-case due to resizing/collisions. This holds in CPython via hash tables; test with large inputs using timeit for real-world validation. Question: Python special methods time complexity chart? Core methods like __getitem__ and __setitem__ ...
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ time-complexities
Python Big O: the time complexities of different data structures in Python - Python Morsels
April 16, 2024 - For example, sets are faster at key lookups than lists, but they have no ordering. Dictionaries are just as fast at key lookups as sets and they maintain item insertion order, but they require more memory.
๐ŸŒ
ESS Institute
essinstitute.in โ€บ home โ€บ blog โ€บ introduction to time complexity in python
Introduction to Time complexity in Python | ESS Institute
June 3, 2023 - Data structures in Python can have ... independent of the amount of input data. For example: When we access an element in an array by index, it has an O(1) time complexity....