Python Sets are a built-in data structure designed to store an unordered collection of unique elements. They are incredibly useful for membership testing, removing duplicates from sequences, and performing mathematical set operations like union, intersection, and difference. In Python, sets offer both performance and clarity when dealing with distinct items in your data.
A Python Set is an unordered collection that does not allow duplicate elements. Sets are mutable, which means you can add or remove elements after their creation. However, the elements themselves must be immutable (like strings, numbers, or tuples).
You can create sets using curly braces {} or the built-in set() constructor.
# Using curly braces fruits = {"apple", "banana", "cherry"} # Using set() function numbers = set([1, 2, 3, 4, 5])
Note: An empty set must be created with set(), not {}, because {} creates an empty dictionary.
colors = {"red", "green", "blue"} # Add a single item colors.add("yellow") # Add multiple items colors.update(["orange", "purple"]) # Remove an item (raises error if not found) colors.remove("green") # Discard an item (does not raise error) colors.discard("black") # Pop a random item colors.pop() # Clear the set colors.clear()
colors = {"red", "blue", "yellow"} print("blue" in colors) # Output: True print(len(colors)) # Output: 3
Operation | Symbol | Method | Description |
---|---|---|---|
Union | | | set1.union(set2) | Returns elements from both sets |
Intersection | & | set1.intersection(set2) | Returns common elements |
Difference | - | set1.difference(set2) | Elements in set1 not in set2 |
Symmetric Difference | ^ | set1.symmetric_difference(set2) | Elements in either set but not both |
a = {1, 2, 3, 4} b = {3, 4, 5, 6} print(a | b) # Union print(a & b) # Intersection print(a - b) # Difference print(a ^ b) # Symmetric Difference
Python also supports set comprehensions, similar to list comprehensions.
# Set comprehension squared = {x*x for x in range(1, 6)} print(squared) # Output: {1, 4, 9, 16, 25}
If you need an immutable version of a set (i.e., cannot be changed after creation), use frozenset().
immutable_set = frozenset(["apple", "banana", "cherry"])
frozenset objects support all set operations but do not support modification methods like add() or remove().
Feature | Sets | Lists | Tuples |
---|---|---|---|
Order | Unordered | Ordered | Ordered |
Duplicates Allowed | No | Yes | Yes |
Mutable | Yes | Yes | No |
Use Case | Unique data | Sequential access | Read-only data |
Python Sets offer an efficient way to store and manipulate unique elements. With their ability to perform mathematical operations, eliminate duplicates, and execute fast membership tests, sets are a go-to tool in a Python programmer’s toolkit. Whether you are cleaning data, managing labels, or optimizing comparisons, sets can make your Python code more concise and performant.
Copyrights © 2024 letsupdateskills All rights reserved