set
    The built-in set data type represents an unordered collection of unique elements. Sets allow for efficient membership testing and set operations like union, intersection, and difference:
>>> fruits = {"apple", "banana", "cherry"}
>>> "banana" in fruits
True
set Constructor
set(iterable)
Arguments
| Argument | Description | 
|---|---|
| iterable | An iterable with elements to be included in the set | 
Note: The values to be included in a set must be hashable objects.
Return Value
- Returns a Python setobject
set Examples
Creating an empty instance of a set:
>>> empty_set = set()
>>> empty_set
set()
Creating instances using a literal:
>>> colors = {"red", "green", "blue"}
>>> colors
{'red', 'blue', 'green'}
Creating an instance using the class constructor with a list as an argument:
>>> numbers = set([1, 2, 3, 4, 5])
>>> numbers
{1, 2, 3, 4, 5}
Checking whether a value is in a set:
>>> "red" in colors
True
Adding or removing elements from a set (mutability):
>>> colors.add("yellow")
>>> colors
{'red', 'green', 'blue', 'yellow'}
>>> colors.remove("green")
>>> colors
{'red', 'blue', 'yellow'}
set Methods
| Method | Description | 
|---|---|
| .add(elem) | Adds an element to the set. | 
| .remove(elem) | Removes an element from the set; raises KeyErrorif not found. | 
| .discard(elem) | Removes an element from the set if present. | 
| .pop() | Removes and returns an arbitrary element; raises KeyErrorif empty. | 
| .clear() | Removes all elements from the set. | 
| .update(iterable) | Updates the set with elements from the iterable. | 
| .intersection_update(iterable) | Updates the set with the intersection of itself and another. | 
| .difference_update(iterable) | Removes all elements of another set from this set. | 
| .symmetric_difference_update(iterable) | Updates the set with the symmetric difference. | 
| .union(*others) | Returns a new set with elements from the set and all others. | 
| .intersection(*others) | Returns a new set with elements common to the set and all others. | 
| .difference(*others) | Returns a new set with elements in the set that are not in the others. | 
| .symmetric_difference(other) | Returns a new set with elements in either the set or other but not both. | 
| .issubset(other) | Returns Trueif the set is a subset of other. | 
| .issuperset(other) | Returns Trueif the set is a superset of other. | 
| .isdisjoint(other) | Returns Trueif the set has no elements in common with other. | 
set Common Use Cases
The most common use cases for set include:
- Removing duplicates from a sequence
- Testing membership efficiently
- Performing mathematical set operations, such as union, intersection, and difference
- Storing unique items without any particular order
set Real-World Example
Consider this example of using a set to find unique visitors to a website:
>>> visitors = ["Alice", "Bob", "Alice", "Charlie", "Bob", "David"]
>>> unique_visitors = set(visitors)
>>> unique_visitors
{'Charlie', 'Bob', 'Alice', 'David'}
This example demonstrates how you can use a set to eliminate duplicate entries and efficiently identify unique elements.
Related Resources
Tutorial
Sets in Python
Learn how to work effectively with Python sets. You’ll define set objects, explore supported operations, and understand when sets are the right choice for your code.
For additional information on related topics, take a look at the following resources:
- Python Set Comprehensions: How and When to Use Them (Tutorial)
- Python's "in" and "not in" Operators: Check for Membership (Tutorial)
- Build a Hash Table in Python With TDD (Tutorial)
- Using Sets in Python (Course)
- Python Sets (Quiz)
- Python Set Comprehensions: How and When to Use Them (Quiz)
- Checking for Membership Using Python's "in" and "not in" Operators (Course)
