Sets

Introduction

Set က Python မှာ list နဲ့ tuple တို့လို Data Structure တစ်ခု။ သူက List လို ပစ္စည်းတွေ သိမ်းတယ်၊ ဒါပေမယ့် သူ့မှာ အထူး စည်းကမ်းတွေ ရှိတယ် - Unique items ပဲ လက်ခံတယ်၊ Index အစီစဉ်တကျ မရှိဘူး။

Set ဆိုတာ?

Set ဆိုတာ List လိုပဲ သေတ္တာ တစ်မျိုးပဲ။ ဒါပေမဲ့ သူက အထာကျတယ်။

Set နဲ့ List ရဲ့ အဓိက ကွာခြားချက် နှစ်ချက် ရှိတယ်။

Set ရဲ့ အဓိက စည်းကမ်း (၂) ချက်

1. Uniqueness (ထပ်တူ မကြိုက်ဘူး):

ဒါ သူ့ရဲ့ အမိုက်ဆုံး အချက်။ Set ထဲကို bro က item တူတွေ (Duplicates) ထပ်ခါတလဲလဲ သွားထည့်လည်း သူ လက်မခံဘူး။ အကုန် ရှင်းထုတ်ပစ်တယ်။

Uniqueness Rule: Set ထဲမှာ တူညီတဲ့ item က တစ်ခုပဲ ရှိနိုင်တယ်။ ထပ်နေတဲ့ကောင်တွေကို အလိုလို ဖယ်ရှားပစ်တယ်။
uniqueness_example.py
# ထပ်နေတဲ့ items တွေ ထည့်ကြည့်မယ် fruits = {"ပန်းသီး", "လိမ္မော်", "ပန်းသီး", "စပျစ်", "လိမ္မော်"} # သူ သိမ်းလိုက်တဲ့အခါ ထပ်နေတဲ့ကောင်တွေ ပြုတ်သွားရော print(fruits)

Output:

Output
{'ပန်းသီး', 'လိမ္မော်', 'စပျစ်'}

2. Unordered (အစဉ်လိုက် မရှိဘူး):

ဒါက အရေးကြီးတယ်။ List (သေတ္တာ) တို့ Tuple (ကျောက်စာ) တို့လို 0, 1, 2 ဆိုတဲ့ တန်းစီနံပါတ် (Index) မရှိဘူး။

သူက အိတ် တစ်လုံးထဲကို ပစ္စည်းတွေ ပစ်ထည့်ထားသလိုပဲ။ ဘယ်ကောင်က အရင်၊ ဘယ်ကောင်က နောက်... ပြောလို့ မရဘူး။

No Indexing! Set မှာ Index မရှိတဲ့အတွက် bro က my_set[0] ဆိုပြီး item ကို လုံးဝယူလို့ မရဘူး။
unordered_example.py
my_set = {5, 2, 8, 1, 9} # Set ကို print ထုတ်တိုင်း အစဉ် မတူနိုင်ဘူး print(my_set) # မတူညီတဲ့ အစဉ်နဲ့ ထွက်နိုင်တယ် # Index နဲ့ ယူလို့ မရဘူး try: print(my_set[0]) # Error! except TypeError as e: print(f"Error: {e}")

Output:

Output
{1, 2, 5, 8, 9} Error: 'set' object is not subscriptable

Set ကို ဘယ်လို ဆောက်မလဲ?

Set ကို ဆောက်ရင် ကွင်း အဖွင့်အပိတ် {} (Curly Braces) သုံးတယ်။

အခြေခံ Set Creation:

basic_set.py
# စာတန်းတွေနဲ့ Set my_set = {"apple", "orange", "banana"} print(my_set) # ဂဏန်းတွေနဲ့ Set (ထပ်နေတာတွေ ထည့်ကြည့်မယ်) numbers_set = {1, 2, 3, 2, 1, 4, 4, 5} # အခု ပြန်ထုတ်ကြည့်... print(numbers_set)

Output:

Output
{'apple', 'orange', 'banana'} {1, 2, 3, 4, 5}

တွေ့လား? ထပ်နေတာတွေ ပြောင်သွားပြီ။

ဗလာ Set (Empty Set):

CRITICAL: Empty Set Warning! bro က ဗလာ Set (Empty Set) ဆောက်ချင်လို့ empty_set = {} လို့ ရေးလိုက်ရင်... အဲ့ဒါ Set မဟုတ်ဘူး။ အဲ့ဒါ Dictionary (နောက်အခန်းမှာ လာမယ်) ဖြစ်သွားလိမ့်မယ်။

ဗလာ Set အစစ် ဆောက်ချင်ရင် set() function ကို သုံးရတယ်။
empty_set_warning.py
# မှားတယ် (Dictionary ဖြစ်သွားပြီ) wrong_way = {} print(type(wrong_way)) # <class 'dict'> # အမှန် (Empty Set) right_way = set() print(type(right_way)) # <class 'set'>

Output:

Output
<class 'dict'> <class 'set'>

set() Function နဲ့ ဆောက်တာ:

set_from_list.py
# List ကနေ Set ဆောက်မယ် my_list = [1, 2, 3, 2, 1] my_set = set(my_list) print(my_set) # {1, 2, 3} # String ကနေ Set ဆောက်မယ် (တစ်လုံးချင်း ခွဲသွားတယ်) char_set = set("hello") print(char_set) # {'h', 'e', 'l', 'o'}

Output:

Output
{1, 2, 3} {'h', 'e', 'l', 'o'}

Duplicate တွေရှင်းမယ်

bro မှာ List တစ်ခု ရှိတယ်၊ အဲ့ထဲမှာ ထပ်နေတဲ့ item တွေ ရှိမယ်။ ရှင်းချင်တယ်။ Set ကို သုံးလိုက်။ တစ်ကြောင်းတည်း နဲ့ ရှင်းရော။

Removing Duplicates List ကနေ Duplicate တွေ ဖယ်ရှားချင်ရင် Set ကို သုံးတာ အမြန်ဆုံး အလွယ်ဆုံး နည်းလမ်းပဲ။
remove_duplicates.py
my_list = ["a", "b", "c", "a", "d", "b"] print(f"Original List: {my_list}") # List ကို set() ထဲ ထည့်ပြီး ကြိတ်လိုက် no_duplicates = set(my_list) print(f"Set (no duplicates): {no_duplicates}") # Set ဖြစ်သွားတာကို List ပြန်ပြောင်းချင်ရင်... clean_list = list(no_duplicates) print(f"Clean List: {clean_list}")

Output:

Output
Original List: ['a', 'b', 'c', 'a', 'd', 'b'] Set (no duplicates): {'a', 'c', 'd', 'b'} Clean List: ['a', 'c', 'd', 'b']

Set Operations

ဒါက Set ရဲ့ အဓိက အလုပ်။ သင်္ချာမှာ သင်ခဲ့ရတဲ့ Set Theory operations တွေက Python မှာ ကစားစရာ။

ကစားဖို့ Set နှစ်ခု အရင် ဆောက်မယ်။

setup_sets.py
# ဘောလုံး ကန်တဲ့ bro တွေ football_bros = {"ညိုကီ", "ချိဖ", "ကို chou", "ဖိုင်တွန်"} # ဂိမ်း ဆော့တဲ့ bro တွေ gaming_bros = {"ကို chou", "ဖိုင်တွန်", "စကီ", "ရှော်ကီ"} print(f"Football: {football_bros}") print(f"Gaming: {gaming_bros}")

ကဲ... ကစားကြမယ်။

1. Union (ပေါင်း) - | ဒါမှမဟုတ် .union()

နှစ်ဖွဲ့လုံးမှာ ရှိသမျှ အကုန် ပေါင်းထည့်။ (ထပ်နေတဲ့ကောင်ကို တစ်ခါပဲ ယူ)

ဘောလုံး ကန်တဲ့ကောင် ဖြစ်စေ၊ ဂိမ်း ဆော့တဲ့ကောင် ဖြစ်စေ အကုန် လာခဲ့။

union_operation.py
# Method 1: | operator သုံးတာ all_bros = football_bros | gaming_bros print(f"Union (|): {all_bros}") # Method 2: .union() method သုံးတာ all_bros2 = football_bros.union(gaming_bros) print(f"Union (.union()): {all_bros2}")

Output:

Output
Union (|): {'ညိုကီ', 'ချိဖ', 'ကို chou', 'ဖိုင်တွန်', 'စကီ', 'ရှော်ကီ'} Union (.union()): {'ညိုကီ', 'ချိဖ', 'ကို chou', 'ဖိုင်တွန်', 'စကီ', 'ရှော်ကီ'}

2. Intersection (ဆုံ) & ဒါမှမဟုတ် .intersection()

နှစ်ဖွဲ့လုံးမှာ တူတူ ပါတဲ့ကောင်တွေပဲ ရွေးထုတ်။

ဘောလုံး လည်း ကန်တယ်၊ ဂိမ်း လည်း ဆော့တယ်။

intersection_operation.py
# Method 1: & operator သုံးတာ both_bros = football_bros & gaming_bros print(f"Intersection (&): {both_bros}") # Method 2: .intersection() method သုံးတာ both_bros2 = football_bros.intersection(gaming_bros) print(f"Intersection (.intersection()): {both_bros2}")

Output:

Output
Intersection (&): {'ကို chou', 'ဖိုင်တွန်'} Intersection (.intersection()): {'ကို chou', 'ဖိုင်တွန်'}

3. Difference - .difference()

ပထမ အဖွဲ့ထဲမှာပဲ ရှိပြီး၊ ဒုတိယ အဖွဲ့ထဲမှာ မရှိတဲ့ကောင်တွေ။

ဘောလုံး ကန်တယ်၊ ဒါပေမဲ့ ဂိမ်း မဆော့ဘူး။

difference_operation.py
# ဘောလုံးပဲ ကန်ပြီး၊ ဂိမ်း မဆော့တဲ့ကောင်တွေ football_only = football_bros - gaming_bros print(f"Football only (-): {football_only}") # .difference() method နဲ့လည်း ရတယ် football_only2 = football_bros.difference(gaming_bros) print(f"Football only (.difference()): {football_only2}") # ပြောင်းပြန်... ဂိမ်းပဲ ဆော့ပြီး၊ ဘောလုံး မကန်တဲ့ကောင်တွေ gaming_only = gaming_bros - football_bros print(f"Gaming only: {gaming_only}")

Output:

Output
Football only (-): {'ညိုကီ', 'ချိဖ'} Football only (.difference()): {'ညိုကီ', 'ချိဖ'} Gaming only: {'စကီ', 'ရှော်ကီ'}

ဒါ အရေးကြီးတယ်။ A - B နဲ့ B - A မတူဘူးနော် bro။

4. Symmetric Difference ^ တစ်နည်း .symmetric_difference()

နှစ်ဖွဲ့လုံးမှာ တူနေတဲ့ကောင်တွေ ဖယ်၊ ကျန်တဲ့ သီးသန့်ကောင်တွေ အကုန်ယူ။

ဘောလုံး ပဲ ကန်တဲ့ကောင်တွေ နဲ့ ဂိမ်း ပဲ ဆော့တဲ့ကောင်တွေ။ (နှစ်ခုလုံး လုပ်တဲ့ကောင်တွေ မယူ)

symmetric_difference_operation.py
# Method 1: ^ operator သုံးတာ one_side_only = football_bros ^ gaming_bros print(f"Symmetric Difference (^): {one_side_only}") # Method 2: .symmetric_difference() method သုံးတာ one_side_only2 = football_bros.symmetric_difference(gaming_bros) print(f"Symmetric Difference (.symmetric_difference()): {one_side_only2}")

Output:

Output
Symmetric Difference (^): {'ညိုကီ', 'ချိဖ', 'စကီ', 'ရှော်ကီ'} Symmetric Difference (.symmetric_difference()): {'ညိုကီ', 'ချိဖ', 'စကီ', 'ရှော်ကီ'}

Set Methods

Set မှာ items တွေ ထည့်ဖို့၊ ဖျက်ဖို့ Methods တွေ ရှိတယ်။

1. .add(item) - Item တစ်ခု ထည့်မယ်

add_method.py
fruits = {"ပန်းသီး", "လိမ္မော်"} print(f"Before: {fruits}") # Item အသစ် ထည့်မယ် fruits.add("စပျစ်") print(f"After add: {fruits}") # ရှိပြီးသား item ထည့်ရင် ဘာမှ မဖြစ်ဘူး fruits.add("ပန်းသီး") print(f"After adding duplicate: {fruits}")

Output:

Output
Before: {'ပန်းသီး', 'လိမ္မော်'} After add: {'ပန်းသီး', 'လိမ္မော်', 'စပျစ်'} After adding duplicate: {'ပန်းသီး', 'လိမ္မော်', 'စပျစ်'}

2. .remove(item) - Item ကို ဖျက်မယ် (မရှိရင် Error)

remove_method.py
fruits = {"ပန်းသီး", "လိမ္မော်", "စပျစ်"} # ရှိတဲ့ item ကို ဖျက်မယ် fruits.remove("လိမ္မော်") print(f"After remove: {fruits}") # မရှိတဲ့ item ကို ဖျက်ရင် KeyError တက်တယ် try: fruits.remove("ငှက်ပျော") except KeyError: print("Error: ငှက်ပျော က Set ထဲမှာ မရှိဘူး!")

Output:

Output
After remove: {'ပန်းသီး', 'စပျစ်'} Error: ငှက်ပျော က Set ထဲမှာ မရှိဘူး!

3. .discard(item) - Item ကို ဖျက်မယ် (မရှိရင် Error မတက်)

discard_method.py
fruits = {"ပန်းသီး", "လိမ္မော်", "စပျစ်"} # ရှိတဲ့ item ကို ဖျက်မယ် fruits.discard("လိမ္မော်") print(f"After discard: {fruits}") # မရှိတဲ့ item ကို ဖျက်ရင် Error မတက်ဘူး fruits.discard("ငှက်ပျော") # No error! print(f"After discarding non-existent: {fruits}")

Output:

Output
After discard: {'ပန်းသီး', 'စပျစ်'} After discarding non-existent: {'ပန်းသီး', 'စပျစ်'}

.remove() နဲ့ .discard() ရဲ့ ကွာခြားချက်:

  • .remove() - Item မရှိရင် KeyError တက်တယ်
  • .discard() - Item မရှိရင် Error မတက်ဘူး (ဘေးကင်းတယ်)

4. .clear() - Set ကို အကုန် ရှင်းမယ်

clear_method.py
fruits = {"ပန်းသီး", "လိမ္မော်", "စပျစ်"} print(f"Before clear: {fruits}") # Set ထဲက items အကုန်လုံးကို ဖျက်မယ် fruits.clear() print(f"After clear: {fruits}")

Output:

Output
Before clear: {'ပန်းသီး', 'လိမ္မော်', 'စပျစ်'} After clear: set()

Subset and Superset

Set တစ်ခုက နောက် Set တစ်ခုရဲ့ အစိတ်အပိုင်းလား၊ ပိုကြီးလား စစ်လို့ရတယ်။

1. .issubset() - Subset စစ်တာ

A က B ရဲ့ အစိတ်အပိုင်းလား? (A ရဲ့ items အားလုံး B မှာ ပါလား?)

issubset_method.py
A = {1, 2} B = {1, 2, 3, 4} C = {5, 6} # A က B ရဲ့ subset လား? print(f"A is subset of B: {A.issubset(B)}") # True # C က B ရဲ့ subset လား? print(f"C is subset of B: {C.issubset(B)}") # False

Output:

Output
A is subset of B: True C is subset of B: False

2. .issuperset() - Superset စစ်တာ

B ရဲ့ items အားလုံး A မှာ ပါလား?

issuperset_method.py
A = {1, 2, 3, 4} B = {1, 2} # A က B ရဲ့ superset လား? print(f"A is superset of B: {A.issuperset(B)}") # True # B က A ရဲ့ superset လား? print(f"B is superset of A: {B.issuperset(A)}") # False

Output:

Output
A is superset of B: True B is superset of A: False

Set ရဲ့ Mutable Nature

Sets are Mutable:

Set ကိုယ်တိုင်က Mutable ဖြစ်တယ် - ဆိုလိုတာက items တွေ ထည့်လို့ရတယ်၊ ဖျက်လို့ရတယ်။

set_mutable.py
my_set = {1, 2, 3} my_set.add(4) # ထည့်လို့ရတယ် my_set.remove(1) # ဖျက်လို့ရတယ် print(my_set)

Output:

Output
{2, 3, 4}

But Set Items Must be Immutable:

Set ထဲက items တွေကတော့ Immutable ဖြစ်ရမယ်။ List (mutable) တွေကို Set ထဲ ထည့်လို့ မရဘူး။

set_items_immutable.py
# ဒါတွေ OK - Immutable items good_set = {1, 2, "hello", (3, 4)} # int, string, tuple - OK! print(f"Good set: {good_set}") # ဒါ မရဘူး - List က mutable try: bad_set = {1, 2, [3, 4]} # TypeError! except TypeError as e: print(f"Error: {e}")

Output:

Output
Good set: {1, 2, 'hello', (3, 4)} Error: unhashable type: 'list'

Set Comprehension

List Comprehension လို Set Comprehension လည်း ရှိတယ်။ Set ကို တစ်ကြောင်းတည်းနဲ့ ဆောက်လို့ရတယ်။

set_comprehension.py
# အခြေခံ Set Comprehension squares = {x**2 for x in range(10)} print(f"Squares: {squares}") # Condition နဲ့ Set Comprehension even_squares = {x**2 for x in range(10) if x % 2 == 0} print(f"Even squares: {even_squares}") # String ကနေ unique characters ယူတာ text = "hello world" unique_chars = {char for char in text if char != " "} print(f"Unique characters: {unique_chars}")

Output:

Output
Squares: {0, 1, 4, 9, 16, 25, 36, 49, 64, 81} Even squares: {0, 4, 16, 36, 64} Unique characters: {'h', 'e', 'l', 'o', 'w', 'r', 'd'}

ဘယ်အခါ Set သုံးမလဲ?

Set သုံးသင့်တဲ့ အချိန်:

  • Uniqueness လိုအပ်ရင်: Duplicate တွေ မလိုချင်ရင် (ဥပမာ: unique user IDs)
  • Membership Testing: Item က Set ထဲမှာ ရှိ/မရှိ မြန်မြန်စစ်ချင်ရင်
  • Mathematical Operations: Union, Intersection တို့ လိုအပ်ရင်
  • Removing Duplicates: List ကနေ duplicates ဖယ်ချင်ရင်
  • No Order Needed: အစီစဉ်တကျဖြစ်နေတာ မလိုအပ်ရင်

Set မသုံးသင့်တဲ့ အချိန်:

  • Order Matters: item တွေအစီစဉ်တကျဖြစ်ဖို့လိုအပ်ရင် အရေးကြီးရင် (List သုံး)
  • Duplicates Needed: တူညီတဲ့ items ကို သိမ်းရမယ်ဆိုရင် (List သုံး)
  • Indexing Required: Index နဲ့ access လုပ်ရမယ်ဆိုရင် (List သုံး)
  • Mutable Items: List တို့ Dictionary တို့ကို သိမ်းရမယ်ဆိုရင်

Examples:

when_to_use_sets.py
# Good use case: Unique emails emails = {"user1@email.com", "user2@email.com", "user1@email.com"} print(f"Unique emails: {emails}") # Duplicates removed! # Good use case: Common friends my_friends = {"အောင်", "မောင်", "ကျော်"} your_friends = {"ကျော်", "စိုး", "ထွန်း"} common_friends = my_friends & your_friends print(f"Common friends: {common_friends}") # Bad use case: Shopping list with quantities (use dict instead) # shopping = {"rice", "fish", "rice"} # Can't track quantities!

Set vs List: နှိုင်းယှဉ်ချက်

Comparison Table
================================================================================ Feature | Set | List ================================================================================ Syntax | {} | [] Order | Unordered | Ordered (indexed) Duplicates | No (Unique only) | Yes Indexing | No | Yes (list[0]) Mutable | Yes (add/remove) | Yes (append/remove) Items | Must be immutable | Can be anything Use Case | Unique items | Ordered collection Math Operations | Yes (|, &, -, ^) | No Speed (membership) | Fast (O(1)) | Slow (O(n)) Example | {1, 2, 3} | [1, 2, 2, 3] ================================================================================

Frozenset

Frozenset ဆိုတာ Immutable Set ပဲ။ Tuple နဲ့ Set ကို ပေါင်းထားသလို။

frozenset_example.py
# Frozenset ဆောက်တာ frozen = frozenset([1, 2, 3, 2, 1]) print(f"Frozenset: {frozen}") # Frozenset ကို ပြင်လို့ မရဘူး try: frozen.add(4) # AttributeError! except AttributeError: print("Frozenset ကို ပြင်လို့ မရဘူး") # Frozenset ကို Set ရဲ့ item အဖြစ် သုံးလို့ရတယ် set_of_sets = {frozenset([1, 2]), frozenset([3, 4])} print(f"Set of frozensets: {set_of_sets}")

Output:

Output
Frozenset: frozenset({1, 2, 3}) Frozenset ကို ပြင်လို့ မရဘူး Set of frozensets: {frozenset({1, 2}), frozenset({3, 4})}

အနှစ်ချုပ်

Set ဆိုတာ:

  • Unordered collection (item ရဲ့ index တွေအစီစဉ်တကျ မရှိ)
  • Unique elements only (ထပ်တဲ့ကောင်တွေ မရှိ)
  • Curly braces {} နဲ့ ဆောက်တယ် (Empty set က set())
  • No indexing (Index နဲ့ access မလုပ်နိုင်)

အဓိက စည်းကမ်း (၂) ချက်:

  • Uniqueness: Duplicate တွေ အလိုလို ဖယ်ရှားပစ်တယ်
  • Unordered: Index မရှိ

Set Operations:

  • Union (|): A | B - နှစ်ဖွဲ့ပေါင်း
  • Intersection (&): A & B - တူတဲ့ကောင်တွေ
  • Difference (-): A - B - A မှာပဲ ရှိတာ
  • Symmetric Difference (^): A ^ B - သီးသန့်ကောင်တွေ

အသုံးများဆုံး Methods:

  • .add() - Item ထည့်
  • .remove() - Item ဖျက် (မရှိရင် error)
  • .discard() - Item ဖျက် (မရှိရင် no error)
  • .clear() - အကုန် ရှင်း
  • .issubset() - Subset စစ်
  • .issuperset() - Superset စစ်

ဘာကြောင့် Set သုံးလဲ?

  • Duplicate ရှင်းဖို့ (List ကနေ)
  • Unique items သိမ်းဖို့
  • Set operations လုပ်ဖို့ (Union, Intersection)
  • Membership testing မြန်ဖို့
Okay? Set က Duplicate မကြိုက်ဘူး၊ item တစ်ခုစီတွက် index အစီစဉ်တကျမရှိဘူး။ ဒါက သူ့ရဲ့ အနှစ်ချုပ်ပဲ bro။ List ကနေ duplicate ရှင်းဖို့၊ Set operations (Union, Intersection) လုပ်ဖို့ အသုံးအများဆုံးပေါ့။

Quick Reference

set_reference.py
# Creation my_set = {1, 2, 3} empty_set = set() from_list = set([1, 2, 2, 3]) # Operations A | B # Union A & B # Intersection A - B # Difference A ^ B # Symmetric Difference # Methods my_set.add(4) my_set.remove(1) my_set.discard(5) my_set.clear() # Subset/Superset A.issubset(B) A.issuperset(B) # Comprehension squares = {x**2 for x in range(10)} # Conversion set(my_list) list(my_set) # No indexing! # my_set[0] # TypeError!