ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
List ဆိုတာ ဈေးဝယ်စာရင်းလိုပါပဲ။ စာရွက်တစ်ရွက်ပေါ်မှာ ပစ္စည်းတွေကို အစဉ်လိုက် ရေးထားပြီး လိုသလို အသစ်ထည့်၊ ဖျက်၊ ပြင်လို့ရပါတယ်။ Python မှာ ["rice", "eggs", "milk"] လို square bracket ထဲမှာ comma ခံပြီး ရေးပါတယ်။
Variable တစ်ခုက value တစ်ခုပဲ သိမ်းနိုင်ပေမယ့် list က value ဘယ်နှခုမဆို သိမ်းနိုင်ပါတယ်။ ကျောင်းသား 30 ယောက်ရဲ့ အမှတ်ကို variable 30 ခု မလုပ်ဘဲ list တစ်ခုထဲ ထည့်ပြီး loop နဲ့ တစ်ခုချင်း ဆက်လုပ်လို့ ရပါတယ်။
Problem → Why → Solution
ပြဿနာ: backup လုပ်ထားတဲ့ list ပါ ပြောင်းသွားတယ်
To-do list ကို မပြင်ခင် backup = tasks လို့ မိတ္တူ ကူးထားပြီး tasks ထဲကို task အသစ် ထည့်လိုက်ပါတယ်။ Undo လုပ်ဖို့ backup ကို ပြန်ကြည့်တော့ task အသစ်က backup ထဲမှာလည်း ပေါ်နေပါတယ်။ Backup က ဘာကိုမှ မကာကွယ်ပေးခဲ့ပါဘူး၊ error လည်း မတက်ပါဘူး။
tasks = ["write report", "email Mya"]
backup = tasks # NOT a copy
tasks.append("call bank")
print(backup) # ['write report', 'email Mya', 'call bank']
print(backup is tasks) # True: one list, two namesဘာကြောင့် ဖြစ်တာလဲ
Python variable တွေက value ကို box ထဲ ထည့်ထားတာ မဟုတ်ဘဲ object တစ်ခုပေါ်မှာ ကပ်ထားတဲ့ နာမည်တံဆိပ်လိုပါ။ backup = tasks က list အသစ် မဖန်တီးဘဲ အဲဒီ list တစ်ခုတည်းမှာ တံဆိပ်နောက်တစ်ခု ကပ်လိုက်တာပါ။ List က mutable (ပြင်လို့ရ) ဖြစ်လို့ နာမည်တစ်ခုကနေ ပြင်ရင် ကျန်တဲ့ နာမည်ကနေလည်း မြင်ရပါတယ်။ မိတ္တူ အစစ်လိုချင်ရင် tasks.copy() ဒါမှမဟုတ် tasks[:] ကို သုံးပါ။
Core concept: list က အစဉ်ရှိပြီး ပြင်လို့ရတဲ့ collection ပါ
List မှာ ဂုဏ်သတ္တိ သုံးခု ရှိပါတယ်။ Ordered ဖြစ်လို့ ထည့်ခဲ့တဲ့ အစဉ်အတိုင်း ရှိနေပါတယ်၊ mutable ဖြစ်လို့ ဖန်တီးပြီးမှ ပြင်လို့ရပါတယ်၊ duplicate value တွေကိုလည်း ခွင့်ပြုပါတယ်။ [1, "two", 3.0, True] လို type မတူတာတွေ ရောထည့်လို့ ရပေမယ့် လက်တွေ့မှာ list တစ်ခုထဲ type တစ်မျိုးတည်း ထားတာက ကိုင်တွယ်ရ ပိုလွယ်ပါတယ်။
List တည်ဆောက်နည်း အမျိုးမျိုး ရှိပါတယ်။ [] က နောက်မှ ဖြည့်ဖို့ ဗလာ list ပါ၊ list("abc") က တခြား collection ကို list ပြောင်းပေးပြီး [0] * 3 က element ကို ထပ်ပြီး [0, 0, 0] ဖြစ်စေပါတယ်။ [1, 2] + [3] က list နှစ်ခုကို ဆက်ပြီး list အသစ် ဖန်တီးပါတယ်။ List ထဲမှာ list တွေ ထပ်ထည့်လို့လည်း ရပြီး row တွေပါတဲ့ table ကို ဒီနည်းနဲ့ ကိုယ်စားပြုလေ့ ရှိပါတယ်။
Index နဲ့ slice က string မှာ လုပ်ခဲ့တဲ့ အတိုင်းပါပဲ — fruits[0] က ပထမ၊ fruits[-1] က နောက်ဆုံး၊ fruits[0:2] က index 0 နဲ့ 1 ပါတဲ့ list အသစ်ပါ။ String နဲ့ ကွာတာက list ကို နေရာမှာတင် ပြင်လို့ရတာပါ — fruits[1] = "mango" က element ကို အစားထိုးပါတယ်။
| Method | လုပ်တဲ့ အလုပ် | Return |
|---|---|---|
| append(x) | x ကို နောက်ဆုံးမှာ ထည့် | None |
| insert(i, x) | x ကို index i မှာ ထည့်၊ ကျန်တာ နောက်ရွှေ့ | None |
| remove(x) | ပထမဆုံး တွေ့တဲ့ x ကို ဖျက် | None (မရှိရင် ValueError) |
| pop(i) | index i (default နောက်ဆုံး) ကို ဖယ်ထုတ် | ဖယ်လိုက်တဲ့ item |
| clear() | Element အားလုံး ဖျက် | None |
List ကို ပြင်တဲ့ method အများစုက list ကို နေရာမှာတင် ပြောင်းပြီး None ကို return ပြန်ပါတယ်။ ဒါကြောင့် fruits = fruits.append("kiwi") လို့ ရေးရင် list ပျောက်ပြီး fruits က None ဖြစ်သွားပါတယ် — string method တွေနဲ့ ပြောင်းပြန်ပါ။ pop() ကတော့ ချွင်းချက်ပါ၊ ဖယ်ထုတ်လိုက်တဲ့ item ကို ပြန်ပေးလို့ next_task = tasks.pop(0) လို သုံးလို့ရပါတယ်။
for item in fruits: က element တစ်ခုချင်းကို အစဉ်လိုက် ပေးပါတယ်။ len(fruits) က element အရေအတွက်ကို ပြန်ပေးပြီး "cherry" in fruits က list ထဲ ပါမပါ စစ်ပါတယ်။
- [...] က list object တစ်ခု ဖန်တီး
- Variable က အဲဒီ object ကို ညွှန်
- Method တွေက object ကို တိုက်ရိုက် ပြင်
- b = a က တံဆိပ်ပဲ ထပ်ကပ်၊ copy မဟုတ်
List ကို ပြင်ရင် အဲဒီ list ကို ညွှန်နေတဲ့ နာမည်တိုင်းက ပြောင်းလဲမှုကို မြင်ရပါတယ်။
အထွေထွေ ပုံစံ
items = [first, second, third]
item = items[index] # read
items[index] = new_value # replace in place
part = items[start:stop] # new list
items.append(value) # add to the end -> None
items.insert(index, value) # add at a position -> None
items.remove(value) # delete first match -> None
taken = items.pop(index) # delete and return the item
copy_of_items = items.copy() # a real, separate copyဖတ်တာက index/slice နဲ့၊ ပြင်တာက method တွေနဲ့ပါ။ ပြင်တဲ့ method တွေက list ကို နေရာမှာတင် ပြောင်းလို့ ရလဒ်ကို ပြန် assign မလုပ်ရပါဘူး၊ pop() ကပဲ item ကို ပြန်ပေးပါတယ်။
Example: fruit list တစ်ခုကို ဖန်တီး၊ ပြင်၊ loop ပတ်
ဒီ example က list တစ်ခုကို ဖန်တီးပြီး ဖတ်တယ်၊ element တစ်ခု အစားထိုးတယ်၊ ထည့်တယ်၊ ဖယ်တယ်။ Step တစ်ခုချင်းပြီးတိုင်း list ကို print ထုတ်ထားလို့ index တွေ ဘယ်လို ရွှေ့သွားလဲ ခြေရာခံလို့ ရပါတယ်။ နောက်ဆုံး loop က ပြင်ထားတဲ့ fruits ပေါ်မှာပဲ ပတ်လို့ အပေါ်က အပြောင်းအလဲ အားလုံးကို ထင်ဟပ်ပြပြီး pop() ပြန်ပေးတဲ့ item ကိုလည်း ပြထားပါတယ်။
# ===== 1. Create a List =====
fruits = ["apple", "banana", "cherry"]
print(f"Original: {fruits}")
print(f"Type: {type(fruits)}")
# ===== 2. Indexing & Slicing =====
print("\n===== Indexing & Slicing =====")
print(f"First item: {fruits[0]}")
print(f"Last item: {fruits[-1]}")
print(f"Slice [0:2]: {fruits[0:2]}")
# ===== 3. Change List Items =====
print("\n===== Modifying =====")
fruits[1] = "mango"
print(f"After change: {fruits}")
# ===== 4. Add Items =====
fruits.append("orange") # Add to end
print(f"After append: {fruits}")
fruits.insert(1, "banana") # Insert at index 1
print(f"After insert: {fruits}")
# ===== 5. Remove Items =====
fruits.remove("apple") # Remove by value
print(f"After remove: {fruits}")
removed = fruits.pop(0) # Remove by index (returns the item)
print(f"After pop: {fruits} (popped {removed!r})")
# ===== 6. Loop Through List =====
print("\n===== Looping =====")
for fruit in fruits:
print(fruit)
# ===== 7. List Length =====
print(f"\nLength: {len(fruits)}")
# ===== 8. Check Membership =====
print(f"\n'cherry' in fruits: {'cherry' in fruits}")Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 14
fruits[1] = "mango"Index 1 မှာ ရှိတဲ့ "banana" ကို "mango" နဲ့ အစားထိုးပါတယ်။ String မှာ ဒီလို ရေးရင် TypeError ဖြစ်ပေမယ့် list က mutable ဖြစ်လို့ ရပါတယ်။
- Line 21
fruits.insert(1, "banana") # Insert at index 1"banana" က index 1 မှာ ဝင်ပြီး mango၊ cherry၊ orange တို့ တစ်နေရာစီ နောက်ရွှေ့သွားပါတယ်။
- Line 25
fruits.remove("apple") # Remove by valueremove() က value နဲ့ ရှာပြီး ပထမဆုံး တွေ့တဲ့ "apple" ကိုပဲ ဖျက်ပါတယ်။ "apple" မရှိရင် ValueError ဖြစ်ပါမယ်။
- Line 28
removed = fruits.pop(0) # Remove by index (returns the item)pop(0) က index 0 က "banana" ကို ဖယ်ထုတ်ပြီး ပြန်ပေးလို့ removed ထဲမှာ သိမ်းထားလို့ရပါတယ်။
- Lines 33-34
for fruit in fruits:Loop က လက်ရှိ list ထဲက element သုံးခုကို အစဉ်လိုက် fruit ထဲ ထည့်ပေးပြီး တစ်ကြောင်းစီ print ထုတ်ပါတယ်။
Original: ['apple', 'banana', 'cherry']
Type: <class 'list'>
===== Indexing & Slicing =====
First item: apple
Last item: cherry
Slice [0:2]: ['apple', 'banana']
===== Modifying =====
After change: ['apple', 'mango', 'cherry']
After append: ['apple', 'mango', 'cherry', 'orange']
After insert: ['apple', 'banana', 'mango', 'cherry', 'orange']
After remove: ['banana', 'mango', 'cherry', 'orange']
After pop: ['mango', 'cherry', 'orange'] (popped 'banana')
===== Looping =====
mango
cherry
orange
Length: 3
'cherry' in fruits: True
append နဲ့ extend၊ sort() နဲ့ sorted()
append() က argument ကို element တစ်ခုအဖြစ် ထည့်လို့ list တစ်ခုကို append လုပ်ရင် list ထဲမှာ list ရှိသွားပါတယ်။ Element တွေကို တစ်ခုချင်း ပေါင်းထည့်ချင်ရင် extend() ကို သုံးပါ။ Sort လုပ်တဲ့အခါမှာလည်း နည်း နှစ်မျိုး ရှိပါတယ် — items.sort() က list ကိုယ်တိုင်ကို စီပြီး None ပြန်ပေးပါတယ်၊ sorted(items) ကတော့ မူရင်းကို မထိဘဲ စီပြီးသား list အသစ်ကို ပြန်ပေးပါတယ်။ reverse=True ထည့်ရင် ကြီးရာကနေ ငယ်ရာ စီပါတယ်။
a = [1, 2]
a.append([3, 4])
print(a) # [1, 2, [3, 4]]
b = [1, 2]
b.extend([3, 4])
print(b) # [1, 2, 3, 4]
scores = [70, 95, 82]
print(sorted(scores, reverse=True), scores) # [95, 82, 70] [70, 95, 82]
print(scores.sort(), scores) # None [70, 82, 95]လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Data ကို အစဉ်လိုက် စုထားရတဲ့ နေရာတိုင်းမှာ list ကို သုံးပါတယ်။ API တစ်ခုက ပြန်ပေးတဲ့ JSON array က Python မှာ list ဖြစ်လာပြီး database query ရလဒ်တွေ၊ file ထဲက line တွေ (file.readlines())၊ string.split() ရဲ့ ရလဒ်တွေ အားလုံး list တွေပါ။ Shopping cart၊ notification တွေ၊ chat message history တွေကိုလည်း list နဲ့ ကိုယ်စားပြုလေ့ ရှိပါတယ်။
Function တစ်ခုထဲကို list ပို့ပြီး function က list ကို append နဲ့ ပြင်လိုက်ရင် caller ရဲ့ list ပါ ပြောင်းသွားပါတယ် — ဒါက problem section က aliasing နဲ့ အတူတူပါပဲ။ ဒါကို ရည်ရွယ်ချက်ရှိရှိ သုံးတာ (add_item(cart, item)) ဒါမှမဟုတ် .copy() နဲ့ ကာကွယ်တာကို သေချာ ရွေးရပါတယ်။ Element သောင်းချီ ရှိလာရင် in နဲ့ ရှာတာ နှေးလာလို့ set ကို ပြောင်းသုံးပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Method ရလဒ်ကို ပြန် assign လုပ်တာ
items = items.append("x") ဒါမှမဟုတ် items = items.sort() က items ကို None ဖြစ်စေပြီး နောက်တစ်ကြောင်းမှာ AttributeError: 'NoneType' object has no attribute ... ဖြစ်ပါတယ်။ items.append("x") လို့ တစ်ကြောင်းတည်း ရေးပါ။
2. b = a ကို copy လို့ ထင်တာ
b = a ပြီးနောက် b ကို ပြင်ရင် a ပါ ပြောင်းပါတယ်။ b = a.copy() ဒါမှမဟုတ် b = list(a) ကို သုံးပါ။ List ထဲမှာ list တွေ ထပ်ရှိရင် copy.deepcopy() လိုမယ်ဆိုတာကိုလည်း မှတ်ထားပါ။
3. ရှိမရှိ မစစ်ဘဲ remove() ခေါ်တာ
items.remove("tea") က "tea" မရှိရင် ValueError: list.remove(x): x not in list ဖြစ်ပါတယ်။ if "tea" in items: နဲ့ အရင်စစ်ပါ။ အလားတူ ဗလာ list ပေါ်မှာ pop() ခေါ်ရင် IndexError ဖြစ်ပါတယ်။
4. Loop ပတ်နေတုန်း list ထဲက ဖျက်တာ
for n in nums: if n < 0: nums.remove(n) က element တစ်ခု ဖျက်တိုင်း ကျန်တာတွေ ရှေ့ရွှေ့လို့ ကပ်လျက် negative နှစ်ခု ရှိရင် ဒုတိယတစ်ခုကို ကျော်သွားပါတယ်။ Error မတက်ဘဲ ရလဒ်ပဲ မှားလို့ သတိထားပါ၊ positives = [] ဖန်တီးပြီး ထားချင်တာကိုပဲ append လုပ်ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- List variable တွေကို fruits၊ scores လို အများကိန်း နာမည် ပေးပြီး loop variable ကို fruit၊ score လို အနည်းကိန်း ပေးပါ။
- မူရင်း အစဉ်ကို ထိန်းချင်ရင် sort() မဟုတ်ဘဲ sorted() ကို သုံးပါ။
- Loop ပတ်နေတုန်း အဲဒီ list ကိုယ်တိုင်ကို remove/append မလုပ်ပါနဲ့၊ list အသစ် တည်ဆောက်ပါ။
Mini challenge
Mini challenge: duplicate မပါတဲ့ ဈေးဝယ်စာရင်း
ဈေးဝယ်စာရင်း list ကို ပြင်ပေးတဲ့ function နှစ်ခု ရေးပြီး copy() နဲ့ sorted() က မူရင်းကို ဘယ်လို ကာကွယ်လဲ ပြပါ။
- add_item(items, item) — strip နဲ့ lower လုပ်ပြီး ဗလာ ဒါမှမဟုတ် ရှိပြီးသား ဆိုရင် မထည့်ရ
- remove_item(items, item) — ရှိရင် ဖျက်ပြီး True၊ မရှိရင် False ပြန်ရမယ် (ValueError မဖြစ်ရ)
- backup = shopping.copy() လုပ်ပြီး shopping ကို ပြင်ကာ backup မပြောင်းကြောင်း ပြရမယ်
- sorted() နဲ့ စီထားတဲ့ list ကို print ထုတ်ပြီး မူရင်း အစဉ် မပြောင်းကြောင်း ပြရမယ်
def add_item(items: list[str], item: str) -> None:
# TODO: clean the item; append only if non-empty and not already present
...
def remove_item(items: list[str], item: str) -> bool:
# TODO: remove and return True if present, otherwise return False
...
shopping = ["rice", "eggs"]
# TODO: add "Milk", " eggs ", "onions" and "" one by one, then print the list
# TODO: remove "eggs" and "tea", printing each result
# TODO: make a backup with .copy(), append "bread" to shopping, print both
# TODO: print sorted(shopping) and then shopping itselfရလဒ်: Program က အောက်ပါအတိုင်း ထုတ်ပေးရပါမယ်: List: ['rice', 'eggs', 'milk', 'onions'] Removed 'eggs': True Removed 'tea': False Shopping: ['rice', 'milk', 'onions', 'bread'] Backup: ['rice', 'milk', 'onions'] Sorted copy: ['bread', 'milk', 'onions', 'rice'] Original order kept: ['rice', 'milk', 'onions', 'bread']
အရိပ်အမြွက် ကြည့်ရန်
if item and item not in items: က ဗလာ string (falsy) နဲ့ duplicate နှစ်ခုလုံးကို တစ်ခါတည်း စစ်ပေးပါတယ်။ add_item က list ကို နေရာမှာတင် ပြင်လို့ return မလိုပါဘူး။
အဖြေ ကြည့်ရန်
def add_item(items: list[str], item: str) -> None:
"""Append item (cleaned, lowercase) unless it is already in the list."""
item = item.strip().lower()
if item and item not in items:
items.append(item)
def remove_item(items: list[str], item: str) -> bool:
"""Remove item if present; return whether something was removed."""
if item in items:
items.remove(item)
return True
return False
shopping = ["rice", "eggs"]
for new in ["Milk", " eggs ", "onions", ""]:
add_item(shopping, new)
print(f"List: {shopping}")
print(f"Removed 'eggs': {remove_item(shopping, 'eggs')}")
print(f"Removed 'tea': {remove_item(shopping, 'tea')}")
backup = shopping.copy()
shopping.append("bread")
print(f"Shopping: {shopping}")
print(f"Backup: {backup}")
print(f"Sorted copy: {sorted(shopping)}")
print(f"Original order kept: {shopping}")Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- List က ordered၊ mutable ဖြစ်ပြီး duplicate တွေကို ခွင့်ပြုတဲ့ collection ပါ။
- append, insert, remove, clear က list ကို နေရာမှာတင် ပြင်ပြီး None ပြန်ပေးကာ pop() က ဖယ်လိုက်တဲ့ item ကို ပြန်ပေးပါတယ်။
- for loop၊ len() နဲ့ in က list နဲ့ အလုပ်လုပ်ရာမှာ အသုံးအများဆုံး tool တွေပါ။
- b = a က copy မဟုတ်ပါဘူး၊ သီးခြား list လိုရင် a.copy() ကို သုံးပါ။
The Python Tutorial — Python Software Foundation