๐งบ Python Set โ Unique, Unordered, Powerful Collection
Introduction ๐
A set in Python is an unordered collection of **unique** elements. Sets automatically remove duplicates and are optimized for fast membership checking. They are ideal for tasks involving uniqueness, mathematical operations, and filtering.
Note
๐ก Sets are defined using curly braces or the set() constructor.
1. Creating Sets ๐งฑ
create_set.py
# Empty set (IMPORTANT: {} is a dict)
empty_set = set()
# Set with values
numbers = {10, 20, 30}
# Set automatically removes duplicates
unique_nums = {1, 2, 2, 3, 3, 4} # {1, 2, 3, 4}
# Mixed data types
mixed = {10, "apple", 3.5, True}Note
โ๏ธ Sets do NOT allow duplicates.
โ๏ธ Sets are unordered โ No indexing.
โ๏ธ Sets are unordered โ No indexing.
2. Accessing Elements โโก๏ธโ๏ธ
Sets do NOT support indexing like lists or tuples.
access_set.py
fruits = {"apple", "banana", "mango"}
for item in fruits:
print(item)Note
โ๏ธ You can loop through the set to access items.
โ๏ธ Order is NOT guaranteed.
โ๏ธ Order is NOT guaranteed.
3. Adding Elements โ
add_set.py
fruits = {"apple", "banana"}
fruits.add("mango")
print(fruits)4. Adding Multiple Items โโ
update_set.py
fruits.update(["grape", "kiwi"])
print(fruits)5. Removing Elements โ
remove() โ Errors if item not found
remove_set.py
fruits.remove("banana")discard() โ Safe removal
discard_set.py
fruits.discard("orange") # No error even if 'orange' not foundpop() โ Removes a random element
pop_set.py
fruits.pop()clear() โ Remove all elements
clear_set.py
fruits.clear()6. Set Operations (Most Powerful Feature) โก
Union ( | ) โ Combine Sets
union_set.py
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # {1, 2, 3, 4, 5}Intersection ( & ) โ Common Elements
intersection_set.py
print(a & b) # {3}Difference ( - ) โ Items in A not in B
difference_set.py
print(a - b) # {1, 2}Symmetric Difference ( ^ ) โ Items NOT in both
symmetric_difference.py
print(a ^ b) # {1, 2, 4, 5}Note
๐ก Set operations are extremely fast due to hashing.
7. Checking Membership ๐
membership_set.py
nums = {10, 20, 30}
print(20 in nums) # True
print(25 not in nums) # True8. Frozen Set โ Immutable Set ๐ง
A frozenset is an immutable version of a set. It cannot be changed after creation.
frozenset.py
fs = frozenset([1, 2, 3])
print(fs)9. Set Comprehension โก
set_comprehension.py
squares = {x*x for x in range(1, 6)}
print(squares)Note
โ๏ธ Similar to list comprehension but produces a set.
10. Real-World Examples ๐
Removing Duplicates From a List
remove_duplicates.py
nums = [1, 2, 2, 3, 3, 4]
unique = list(set(nums))
print(unique)Checking Common Interests
common_interests.py
person1 = {"music", "sports", "coding"}
person2 = {"coding", "movies"}
print(person1 & person2) # intersectionFiltering Unique Words
filter_words.py
sentence = "apple banana apple mango banana"
words = set(sentence.split())
print(words)Conclusion ๐
>>โSets bring speed, uniqueness, and mathematical power to Python โ simple yet incredibly useful.โ โจ
You now fully understand Python Sets! Want the next topic? Try Dictionary, List Comprehension, Functions, or File Handling. Just tell me! ๐