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
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 KeyError if not found. |
.discard(elem) |
Removes an element from the set if present. |
.pop() |
Removes and returns an arbitrary element; raises KeyError if empty. |
.clear() |
Removes all elements from the set. |
.copy() |
Returns a shallow copy of the set. |
.update(*others) |
Updates the set with elements from all the others. |
.intersection_update(*others) |
Updates the set, keeping only elements found in it and all others. |
.difference_update(*others) |
Removes all elements found in others from this set. |
.symmetric_difference_update(other) |
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 True if the set is a subset of other. |
.issuperset(other) |
Returns True if the set is a superset of other. |
.isdisjoint(other) |
Returns True if 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 to work effectively with Python sets: define set objects, explore the supported operations, and know when sets fit 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)
- Using Sets in Python (Course)
- Records and Sets: Selecting the Ideal Data Structure (Course)
- Python's "in" and "not in" Operators: Check for Membership (Tutorial)
- Build a Hash Table in Python With TDD (Tutorial)
- 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)
- Build a Hash Table in Python With TDD (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.