set
Python's built-in unordered collection of unique items, implemented as a hash table that stores keys without values.
In more detail
Checking whether an item is in a set costs O(1) on average, against O(n) for a list, which makes sets the right tool for membership tests and duplicate removal. Items must be hashable. Sets cannot answer range queries or give items in sorted order.
Related terms
- dictionary — A Python collection that stores values under keys, written in curly brackets, such as {"tea": 250}.
- membership test — Asking whether a collection contains a given item, written `x in collection` in Python.
- hashable — Describes a Python object that has a hash value that never changes during its lifetime and can be compared for equality, so it can be a dict key or set member.
- hash table — A data structure that stores key-value pairs in an array and uses a hash of each key to jump almost directly to its slot.
Taught in
- Python Dicts And Sets Under The Hood — Algorithms & Data Structures for Working Engineers
- Bloom Filters: Membership Without The Data — Algorithms & Data Structures for Working Engineers
- Linked Lists: Nodes, Pointers And Sentinels — Algorithms & Data Structures for Working Engineers
- Searching With Hashes Versus Trees — Algorithms & Data Structures for Working Engineers
- Depth-First Search, Cycles And Topological Order — Algorithms & Data Structures for Working Engineers
- Backtracking: Search With Pruning — Algorithms & Data Structures for Working Engineers