Sets
A set in Python is a collection that is unordered, mutable, and contains unique elements (no duplicates). Sets are useful when you need to store a group of items and ensure that no duplicates are present.
Key Features of Sets
- Unordered: The elements in a set do not have a defined order.
- Unique Elements: A set cannot have duplicate values.
- Mutable: You can add or remove items from a set.
- Cannot Contain Mutable Data Types: Items in a set must be immutable (e.g., numbers, strings, tuples).
Creating a Set
You can create a set using curly braces {} or the set() function.
Examples:
Python
Copy
# Empty set (use set(), not {}) empty_set = set() # Set with numbers numbers = {1, 2, 3, 4} # Set with mixed data types mixed_set = {1, "hello", 3.14} # Creating a set from a list list_set = set([1, 2, 2, 3, 3, 4]) # Removes duplicates print(list_set) # Output: {1, 2, 3, 4}
Accessing Set Elements
Since sets are unordered, you cannot access elements using indexing or slicing. Instead, you can use a loop to iterate through the elements.
Example:
Python
Copy
fruits = {"apple", "banana", "cherry"} for fruit in fruits: print(fruit)
Output (order may vary):
apple
banana
cherry
Adding and Removing Elements
Adding Elements
- Use add() to add a single item.
Python
Copy
numbers = {1, 2, 3} numbers.add(4) print(numbers) # Output: {1, 2, 3, 4}
- Use update() to add multiple items.
Python
Copy
numbers.update([5, 6]) print(numbers) # Output: {1, 2, 3, 4, 5, 6}
Removing Elements
- Use remove() to remove a specific item (raises an error if the item doesn't exist).
Python
Copy
numbers.remove(2) print(numbers) # Output: {1, 3, 4, 5, 6}
- Use discard() to remove an item without raising an error if it doesn’t exist.
Python
Copy
numbers.discard(7) # No error if 7 is not in the set
- Use pop() to remove a random item.
Python
Copy
random_item = numbers.pop() print(random_item) # Output: Randomly chosen item
Set Operations
Sets provide powerful methods for mathematical operations like union, intersection, and difference.
Examples:
Python
Copy
set_a = {1, 2, 3, 4} set_b = {3, 4, 5, 6} # Union: Combine both sets (all unique elements) print(set_a | set_b) # Output: {1, 2, 3, 4, 5, 6} # Intersection: Common elements in both sets print(set_a & set_b) # Output: {3, 4} # Difference: Elements in set_a but not in set_b print(set_a - set_b) # Output: {1, 2} # Symmetric Difference: Elements in either set_a or set_b, but not both print(set_a ^ set_b) # Output: {1, 2, 5, 6}
Set Methods
Method | Description | Example |
---|---|---|
add(x) | Adds item x to the set | set_a.add(5) |
remove(x) | Removes x from the set (raises error if not found) | set_a.remove(3) |
discard(x) | Removes x (does not raise error if not found) | set_a.discard(7) |
pop() | Removes and returns a random element | set_a.pop() |
update(iterable) | Adds multiple items to the set | set_a.update([5, 6]) |
union() | Returns the union of sets | set_a.union(set_b) |
intersection() | Returns the intersection of sets | set_a.intersection(set_b) |
difference() | Returns the difference between sets | set_a.difference(set_b) |
symmetric_difference() | Returns elements in either set, but not both | set_a.symmetric_difference(set_b) |
issubset() | Checks if one set is a subset of another | set_a.issubset(set_b) |
issuperset() | Checks if one set is a superset of another | set_a.issuperset(set_b) |
Frozen Sets
A frozen set is an immutable version of a set. You cannot modify its elements after creation.
Example:
Python
Copy
frozen = frozenset([1, 2, 3, 4]) print(frozen) # Output: frozenset({1, 2, 3, 4}) # Immutable: cannot add or remove items # frozen.add(5) # Error