A set in Python is an unordered, mutable, and unindexed collection of unique elements.
Sets are primarily used to remove duplicates, perform mathematical set operations, and check membership efficiently.
Sets are defined using curly braces {} or the set() constructor.
πΉ Key Properties of Sets
- Unordered: The elements have no defined order.
- Unique: Duplicate values are automatically removed.
- Mutable: You can add or remove elements after creation.
- Heterogeneous: Can contain different data types.
- Non-indexed: Elements cannot be accessed via index like lists.
πΉ Common Set Functions & Methods
| Function / Method | Description | Example |
|---|---|---|
set() | Creates a new empty set or converts an iterable into a set. | set([1, 2, 3]) β {1, 2, 3} |
add(x) | Adds an element x to the set. | {1, 2}.add(3) β {1, 2, 3} |
update(iterable) | Adds multiple elements to the set. | {1, 2}.update([3, 4]) β {1, 2, 3, 4} |
remove(x) | Removes the element x; raises error if not found. | {1, 2, 3}.remove(2) β {1, 3} |
discard(x) | Removes the element if present (no error if absent). | {1, 2}.discard(3) |
pop() | Removes and returns a random element. | {1, 2, 3}.pop() |
clear() | Removes all elements from the set. | {1, 2, 3}.clear() β set() |
copy() | Returns a shallow copy of the set. | b = a.copy() |
union(set2) | Returns a new set with all unique elements from both sets. | {1, 2}.union({2, 3}) β {1, 2, 3} |
intersection(set2) | Returns common elements between sets. | {1, 2, 3}.intersection({2, 3, 4}) β {2, 3} |
difference(set2) | Returns elements present in one set but not the other. | {1, 2, 3}.difference({2, 3, 4}) β {1} |
symmetric_difference(set2) | Returns elements not common to both sets. | {1, 2, 3}.symmetric_difference({2, 3, 4}) β {1, 4} |
issubset(set2) | Returns True if all elements are in another set. | {1, 2}.issubset({1, 2, 3}) β True |
issuperset(set2) | Returns True if the set contains all elements of another. | {1, 2, 3}.issuperset({1, 2}) β True |
isdisjoint(set2) | Returns True if sets have no common elements. | {1, 2}.isdisjoint({3, 4}) β True |
π§© Example 1: Creating and Adding Elements
my_set = {1, 2, 3}
my_set.add(4)
my_set.update([5, 6])
print("Set after adding:", my_set)Output:
Set after adding: {1, 2, 3, 4, 5, 6}π§© Example 2: Removing Elements
nums = {1, 2, 3, 4}
nums.remove(3)
nums.discard(5) # No error even if element not present
print("After removal:", nums)Output:
After removal: {1, 2, 4}π§© Example 3: Set Union and Intersection
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print("Union:", set1.union(set2))
print("Intersection:", set1.intersection(set2))Output:
Union: {1, 2, 3, 4, 5}
Intersection: {3}π§© Example 4: Difference and Symmetric Difference
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print("Difference:", a.difference(b))
print("Symmetric Difference:", a.symmetric_difference(b))Output:
Difference: {1, 2}
Symmetric Difference: {1, 2, 5, 6}π§© Example 5: Checking Subset and Superset
A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
print("A is subset of B:", A.issubset(B))
print("B is superset of A:", B.issuperset(A))Output:
A is subset of B: True
B is superset of A: Trueπ§© Example 6: Clearing and Copying Sets
fruits = {"apple", "banana", "cherry"}
copy_set = fruits.copy()
fruits.clear()
print("Cleared Set:", fruits)
print("Copied Set:", copy_set)Output:
Cleared Set: set()
Copied Set: {'apple', 'banana', 'cherry'}πΉ In Summary
- Sets are unordered collections of unique elements.
- Commonly used for membership tests and mathematical operations (union, intersection, etc.).
- Key methods include
add(),remove(),union(),intersection(),difference(), andclear(). - Fast and efficient for checking presence or performing mathematical comparisons between datasets.
