set (Python)
An unordered collection of distinct values, written {1, 2, 3} or made with set(), where each value appears only once.
In more detail
Converting a list to a set removes its duplicates, and testing membership with in is very fast however big the set is. Sets have no order and no indices, and their items must be hashable. {} makes an empty dictionary, so write set() for an empty set.
Example
set("mississippi") # {"m", "i", "s", "p"}Related terms
- list — An ordered, changeable collection of values written in square brackets, such as [3, 1, 4].
- dictionary — A Python collection that stores values under keys, written in curly brackets, such as {"tea": 250}.
- 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.
- in operator — The operator that tests whether a value is contained in a string, list, tuple, set or dictionary, giving True or False.
Taught in
- Appending To Lists, Sets, Constructors — Hands-on Python: Complete Video Course & Book
- Modules And The Import System — Hands-on Python: Complete Video Course & Book