Python: set() — Don’t Underestimate
When we think of Python data structures, we often jump to lists, dictionaries, or tuples. But there’s one silent hero that can help clean up your code and make operations faster: set().
In this article, we’ll explore why set() is more powerful than it looks and how you can use it effectively with simple examples.
In summary:
- Unordered: data ที่ใส่เข้าไป มันไม่ได้ถูก order นะ (เข้าถึงแบบ index ไม่ได้)
- Unchangeable: Set are immutable ดังนั้นจะต่างจาก list ที่แก้ไขค่าไม่ได้
- Heterogeneous: Contain ได้ all types like in List (อ่านว่า เฮ๊เดอรีจีเนียส adj = ไม่เป็นเนื้อเดียวกัน)
- Unique: Not allow duplicates items (ข้อดีของมัน ที่ผมใช้กรอง data)
####################################################################
▪ | = union (เอาทั้งหมด)
▪ & = intersection (เอาที่ซำ้กัน)
▪ - = defference (เอาอันหน้าที่ไม่มีในอันหลัง)
▪ ^ = symmetric difference (เอาที่ไม่ซ้ำกัน)
- add() # เป็นการ add ทีละ 1 element
- update() # เป็นการ add เป็น collection
- remove() # remove ทีละ 1 element (ถ้าไม่เจอ element จะพังนะ)
- discard() # remove ทีละ 1 element เช่นกัน (แต่ถ้าไม่เจอ จะไม่พัง)
- pop() # เป็นการ remove ออกทีละ 1 element จากซ้ายมือ
- clear() # clear to set()
Use case ในการวนลูป add or checking value จะเห็นว่าการใช้ set() ทำงานได้รวดเร็วกว่า List แบบเยอะมากๆ
A set in Python is a collection of unique items. Unlike lists, sets do not allow duplicate values, and they are unordered.
my_list = [1, 2, 2, 3, 3]
my_set = set(my_list)
print(my_set) # Output: {1, 2, 3}Why Use set()?
1. Remove Duplicates Quickly
- มันจะเก็บ element แรกที่เจอเอาไว้นะ ช่วยให้เรา clean data ได้ง่ายขึ้น
nums = [1, 2, 2, 3, 4, 4, 5]
unique = list(set(nums))
print(unique) # [1, 2, 3, 4, 5]2. Fast Membership Check
- อันนี้ทำได้เหมือนกับ list() เลยนะคับ เป็นวิธีการเช็คว่ามี element นี้ไหม
my_set = {1, 2, 3}
print(2 in my_set) # True
print(4 in my_set) # FalseSet lookups are faster than list lookups, especially with large data.
Common set Operations
Union หรือ |
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B) # {1, 2, 3, 4, 5}Intersection หรือ &
print(A & B) # {3}Difference หรือ -
- เป็นการเอา A ที่ไม่อยู่ใน B
print(A - B) # {1, 2}Symmetric Difference หรือ ^
- เป็นการเลือกเอาค่าที่แตกต่างกันของทั้ง 2 sets
- ค่าที่มีใน set ใด set หนึ่งเท่านั้น แต่ไม่มีร่วมกัน (เข้าใจมากขึ้นไหมคับ)
- พูดง่ายๆ
เอาค่าที่ไม่ซ้ำกันของทั้ง 2 sets
ผมใช้บ่อยเลยนะ เช่น เมื่อต้องหา invoice numbers ขนาดใหญ่ อยากรู้ว่ามีส่วนไหนที่แตกต่างกัน
print(A ^ B) # {1, 2, 4, 5}
###########################
# หรือ อีกตัวอย่างนะ
a = {1,2,3,4}
b = {1,2,4,5}
a ^ b
# {3, 5} จะได้ค่าที่ไม่มีในทั้ง 2Symmetric Difference means the items that are in either set, but not in both. In other words, it finds values that are unique to each set.
Get Grassroot Engineer’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 ^ set2) # {1, 2, 4, 5} → 3 is removed because it's in bothReal Use Case: Find Uncommon Items
- ที่ผมใช้บ่อยในการหาค่าที่ไม่ซ้ำกันของ 2 sets
# Find items that are only in one list
# can use ^ instead of .symmetric_difference
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7]
diff = list(set(list1).symmetric_difference(set(list2)))
print(diff) # [1, 2, 3, 6, 7]This is helpful when comparing logs, lists of users, or any non-duplicate comparisons.
Other Helpful set() Methods
Adding & Updating
add()update()
# Adding
s = {1, 2}
s.add(3) # เพิ่มได้แค่ 1 element
print(s)
# {1, 2, 3}
#########################
# Updating
s.update([4, 5]) # ถ้าจะเพิ่มเป็นชุดต้องใช้ update() แทน
print(s)
# {1, 2, 3, 4, 5}Removing Elements
remove()discard()pop()clear()
s = {1, 2, 3, 4, 5}
s.remove(6) # ถ้า remove ไม่ได้จะพังนะ เคสนี้พังเพราะไม่มี 6
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 6
s.remove(5)
print(s) # {1, 2, 3, 4}
########################
s = {1, 2, 3, 4}
s.discard(3)
print(s) # {1, 2, 4}
s.discard(9) # แม้ว่าจะไม่มี 9 ก็ไม่พังนะ ถ้าใช้ discard
print(s) # {1, 2, 4}
########################
s = {1, 2, 4}
s.pop() # 1 (ในการ pop ไม่ต้องระบุ argument นะ มันจะเอา element แรกฝั่งซ้ายมือออกเองเลย)
print(s) # {2, 4}
########################
s = {2, 3, 4, 1, 5}
s.clear() # อันนี้น่ากลัวมากนะ ต้องระหวังด้วย เพราะเป็นการ clear set ทั้งหมดเลย
print(s) # set()Set Comparison
set1 = {1,2,3}
set2 = {0,1,2,3,4,5,6}
print(set1.isdisjoint(set2)) # False เพราะเป็นการเช็คว่า "ไม่มีอะไรเหมือนกันใช่ไหม
# ซึ่งคำตอบคือไม่ใช่ เพราะมี 1,2,3 เหมือนกัน"
print(set1.issubset(set2)) # True เพราะ set1 อยู่ใน set2 จ้า
print(set1.issuperset(set2)) # False เพราะ set1 ไม่ได้เป็น superset ของ set2In-Place Operations
- เป็นการ update หรือ assign ค่าเข้าไปใน set1 เลยนั่นเอง จากที่การใช้
-, &, ^แบบธรรมดาจะยังไม่ได้ assign ค่า
set1 = {1, 2, 3}
set2 = {3, 4}
# -=
set1.difference_update(set2) # set1 becomes {1, 2}
# &=
set1.intersection_update(set2) # set1 becomes {}
# ^=
set1.symmetric_difference_update(set2) # symmetric diff and assign to set1Summary
set()helps remove duplicates (ใช้บ่อย)- It allows fast comparison and membership tests
- Great for mathematical set operations
- Includes many useful methods for updating, comparing, and managing data
Don’t underestimate set(). It may look simple, but it can be a powerful part of your Python toolkit.
Refs:
https://realpython.com/python-sets/
If you think it’s useful for you, just clap your hands 👏 to be encouraged me.
GRASSROOT ENGINEER 😘








