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
set
object
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:
>>> 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. |
.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 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 the set
include:
- Removing duplicates from a sequence
- Efficient membership testing
- Performing mathematical set operations, such as union, intersection, and difference
- Storing unique items without any particular order
set
Real-World Example
Consider an 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, efficiently identifying unique elements.
Related Resources
Tutorial
Sets in Python
In this tutorial you'll learn how to work effectively with Python's set data type. You'll see how to define set objects in Python and discover the operations that they support and by the end of the tutorial you'll have a good feel for when a set is an appropriate choice in your own programs.
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)