Dictionaries

Introduction

ဒီတစ်ခါ Dictionaries အကြောင်းကို ပြောပြပါမယ်။ ဒီကောင်က Python မှာ အရေးကြီးတဲ့ နှလုံးသားလေးပေါ့။ List, Tuple, Set တို့နဲ့ လုံးဝ မတူတဲ့ အထာ နဲ့ လာတာ။

List တို့က item ယူချင်ရင် [0], [1] ဆိုတဲ့ Index နံပါတ် နဲ့ ယူရတယ်။ ဘယ်နှခုမြောက် ဆိုတာ သိရတယ်။

Dictionary က အဲ့လို နံပါတ်နဲ့ မဟုတ်ဘူး။ သူက နာမည် (Key) နဲ့ ခေါ်တာ။

Dictionary: Dictionary က Key-Value Pairs သုံးပြီး data တွေကို structured အနေနဲ့ သိမ်းတယ်။ JSON, APIs, Configuration files အကုန် Dictionary structure ကို သုံးတယ်။

Dictionary ဆိုတာ ဘာလဲပေါ့?

Dictionary ကို ရှင်းပြရမယ်ဆိုရင်... သူက အဘိဓာန် စာအုပ်ကြီး (ဒါမှမဟုတ် ဖုန်းစာအုပ် Phone Book) နဲ့ တူတယ်။

  • Key (သော့): ဒါက bro ရှာချင်တဲ့ စကားလုံး (ဥပမာ: "Apple")
  • Value (တန်ဖိုး): ဒါက အဲ့ဒီစကားလုံးရဲ့ အဓိပ္ပာယ် (ဥပမာ: "ပန်းသီး")

Key တစ်ခုမှာ Value တစ်ခု... စုံတွဲလိုက် ရှိတယ်။ ဒါကို Key-Value Pair လို့ခေါ်တယ်။

Key/Value Pairs

Dictionary မှာ data တွေကို Key-Value Pair အဖြစ် သိမ်းတယ်။

key_value_concept.py
# အဘိဓာန် ဥပမာ # Key: "Apple" → Value: "ပန်းသီး" # Key: "Book" → Value: "စာအုပ်" # ဖုန်းစာအုပ် ဥပမာ # Key: "အောင်အောင်" → Value: "09-123-456" # Key: "မောင်မောင်" → Value: "09-789-012"

Key က unique ဖြစ်ရမယ်။ တူနေတဲ့ Key နှစ်ခု မရှိရဘူး/မရှိနိုင်ဘူး။ Value ကတော့ တူလို့ရတယ်။

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

Set လိုပဲ ကွင်း အဖွင့်အပိတ် {} (Curly Braces) သုံးတယ်။ ဒါပေမဲ့ အထဲမှာ key: value (Key ရယ်၊ colon (:) ရယ်၊ Value ရယ်) စုံတွဲတွေ ထည့်ပေးရတယ်။

CRITICAL: {} က Empty Dictionary! {} က Empty Dictionary ပဲ၊ Empty Set မဟုတ်ဘူး။ Empty Set လိုချင်ရင် set() သုံးရမယ်နော် မရော နဲ့။

ဥပမာ: bro တစ်ယောက်ရဲ့ Profile ဆောက်ကြည့်မယ်

profile_example.py
my_profile = { "name": "အောင်အောင်", "age": 25, "job": "Developer", "is_cool": True, "skills": ["Python", "Java", "SQL"] } print(my_profile)

Output:

Output
{'name': 'အောင်အောင်', 'age': 25, 'job': 'Developer', 'is_cool': True, 'skills': ['Python', 'Java', 'SQL']}

တွေ့လား?

  • "name" ဆိုတဲ့ Key မှာ "အောင်အောင်" ဆိုတဲ့ Value ရှိတယ်
  • "age" ဆိုတဲ့ Key မှာ 25 ဆိုတဲ့ Value ရှိတယ်
  • "skills" ဆိုတဲ့ Key မှာ List တစ်ခုလုံးက Value ဖြစ်နေတယ်

Dict ထဲမှာ List, Tuple, တခြား Dict တွေပါပြန်ထည့်လို့ရတယ်...။

အခြား Creation နည်းလမ်းများ:

other_creation_methods.py
# Empty Dictionary empty_dict = {} print(type(empty_dict)) # <class 'dict'> # dict() constructor သုံးတာ person = dict(name="မောင်မောင်", age=30, city="Yangon") print(person)

Output:

Output
<class 'dict'> {'name': 'မောင်မောင်', 'age': 30, 'city': 'Yangon'}

Data ယူတဲ့ ပုံစံသစ်

List မှာတုန်းက my_list[0] ဆိုပြီး index နဲ့ ယူတယ်။ Dictionary မှာတော့ my_dict["key_name"] key နဲ့ ယူတယ်။

Square Brackets [] နဲ့ Access လုပ်တာ:

accessing_with_brackets.py
my_profile = { "name": "အောင်အောင်", "age": 25, "job": "Developer" } # အောင်အောင် ရဲ့ အလုပ်ကို သိချင်ရင် print(my_profile["job"]) # သူ့အသက်ကို သိချင်ရင် print(my_profile["age"])

Output:

Output
Developer 25

KeyError - မရှိတဲ့ Key ကို Access လုပ်ရင်:

KeyError Warning! Dictionary မှာ မရှိတဲ့ Key ကို access လုပ်ရင် KeyError တက်တယ်။
keyerror_example.py
my_profile = {"name": "အောင်အောင်", "age": 25} # မရှိတဲ့ key ကို access လုပ်ကြည့်မယ် try: print(my_profile["city"]) # KeyError! except KeyError: print("Error: 'city' key က Dictionary မှာ မရှိဘူး!")

Output:

Output
Error: 'city' key က Dictionary မှာ မရှိဘူး!

.get() Method - ဘေးကင်းတဲ့ Access:

Safe Access with .get(): .get() method က KeyError မတက်ဘူး။ Key မရှိရင် None (ဒါမှမဟုတ် default value) ပြန်ပေးတယ်။
get_method.py
my_profile = {"name": "အောင်အောင်", "age": 25} # .get() နဲ့ access လုပ်မယ် (Error မတက်ဘူး) city = my_profile.get("city") print(f"City: {city}") # None # Default value ပေးလို့ရတယ် city = my_profile.get("city", "Unknown") print(f"City: {city}") # Unknown # ရှိတဲ့ key ဆိုရင် value ကို ပြန်ပေးတယ် name = my_profile.get("name") print(f"Name: {name}") # အောင်အောင်

Output:

Output
City: None City: Unknown Name: အောင်အောင်

[] vs .get() - ဘယ်အချိန် ဘာသုံးမလဲ?

  • [] - Key ရှိမှာ သေချာရင် သုံးပေါ့
  • .get() - Key ရှိ/မရှိ မသေချာရင်၊ default value လိုအပ်တဲ့ခါ

Data ထည့်တာ၊ ပြင်တာ

List ထက်တောင် လွယ်သေးတယ်။

Value ကို ပြင်တာ:

modifying_values.py
my_profile = { "name": "အောင်အောင်", "age": 25, "job": "Developer" } # အသက် ကို ပြင်မယ် my_profile["age"] = 26 # 25 ကနေ 26 ဖြစ်သွားပြီ print(my_profile)

Output:

Output
{'name': 'အောင်အောင်', 'age': 26, 'job': 'Developer'}

Key အသစ် ထပ်ထည့်တာ:

adding_new_keys.py
my_profile = { "name": "အောင်အောင်", "age": 25 } # မြို့ (city) ဆိုတဲ့ Key အသစ် ထပ်ထည့်မယ် (မရှိဘူး အရင်က) my_profile["city"] = "Yangon" # အခု ပြန်ထုတ်ကြည့် print(my_profile)

Output:

Output
{'name': 'အောင်အောင်', 'age': 25, 'city': 'Yangon'}

Item တွေ ဖျက်မယ်

1. del keyword - Key ကို ဖျက်မယ်

del_keyword.py
my_profile = { "name": "အောင်အောင်", "age": 25, "city": "Yangon" } # city key ကို ဖျက်မယ် del my_profile["city"] print(my_profile)

Output:

Output
{'name': 'အောင်အောင်', 'age': 25}

2. .pop() Method - ဖျက်ပြီး Value ကို ပြန်ယူမယ်

pop_method.py
my_profile = { "name": "အောင်အောင်", "age": 25, "city": "Yangon" } # city key ကို ဖျက်ပြီး value ကို ယူမယ် removed_city = my_profile.pop("city") print(f"Removed city: {removed_city}") print(f"Profile: {my_profile}") # .pop() နဲ့ default value ပေးလို့ရတယ် country = my_profile.pop("country", "Myanmar") print(f"Country: {country}")

Output:

Output
Removed city: Yangon Profile: {'name': 'အောင်အောင်', 'age': 25} Country: Myanmar

Key ရှိ/မရှိ စစ်မယ်

Method 1: in operator သုံးတာ

in_operator.py
my_profile = { "name": "အောင်အောင်", "age": 25 } # Key ရှိ/မရှိ စစ်မယ် if "name" in my_profile: print("name key ရှိတယ်") if "city" not in my_profile: print("city key မရှိဘူး")

Output:

Output
name key ရှိတယ် city key မရှိဘူး

Method 2: .get() နဲ့ None စစ်တာ

get_check.py
my_profile = {"name": "အောင်အောင်", "age": 25} # .get() က None ပြန်ပေးရင် key မရှိဘူး if my_profile.get("city") is None: print("city key မရှိဘူး")

Output:

Output
city key မရှိဘူး

Dictionary Methods

Dictionary နဲ့ အလုပ်လုပ်ဖို့ အသုံးတည့်တဲ့ Methods တွေ ရှိတယ်။

1. .keys() - Key တွေ ချည်းပဲ ဆွဲထုတ်မယ်

ဒါက Dictionary ထဲက Key (သော့) တွေကိုပဲ ဆွဲထုတ်ပေးတယ်။ ဖုန်းစာအုပ်ထဲက နာမည် တွေချည်းပဲ အကုန်ပေး ဆိုသလိုပဲ။

keys_method.py
my_profile = { "name": "အောင်အောင်", "age": 25, "job": "Developer", "city": "Yangon" } # Key တွေ အကုန် ဆွဲထုတ်မယ် all_keys = my_profile.keys() print(all_keys) print(list(all_keys)) # List အဖြစ် ပြောင်းလို့ရတယ်

Output:

Output
dict_keys(['name', 'age', 'job', 'city']) ['name', 'age', 'job', 'city']

2. .values() - Value တွေ ချည်းပဲ ဆွဲထုတ်မယ်

ဒါက Dictionary ထဲက Value (တန်ဖိုး) တွေကိုပဲ ဆွဲထုတ်မယ်။ ဖုန်းစာအုပ်ထဲက ဖုန်းနံပါတ် တွေချည်းပဲ အကုန်ပေး ဆိုသလိုပဲ။

values_method.py
my_profile = { "name": "အောင်အောင်", "age": 25, "job": "Developer" } # Value တွေ အကုန် ဆွဲထုတ်မယ် all_values = my_profile.values() print(all_values) print(list(all_values))

Output:

Output
dict_values(['အောင်အောင်', 25, 'Developer']) ['အောင်အောင်', 25, 'Developer']

3. .items() - စုံတွဲလိုက် အကုန် ဆွဲထုတ်မယ်

ဒါက အသုံးအဝင်ဆုံးပဲ။ သူက Key ရော Value ရော စုံတွဲလိုက် (Tuple အနေနဲ့) ဆွဲထုတ်ပေးတယ်။ ဖုန်းစာအုပ်ထဲက နာမည်ရော ဖုန်းနံပါတ်ရော စုံတွဲလိုက် အကုန်ပေးလို့ ပြောလိုက်သလိုပေါ့။

items_method.py
my_profile = { "name": "အောင်အောင်", "age": 25, "job": "Developer" } # Key-Value pairs အကုန် ဆွဲထုတ်မယ် all_items = my_profile.items() print(all_items) print(list(all_items))

Output:

Output
dict_items([('name', 'အောင်အောင်'), ('age', 25), ('job', 'Developer')]) [('name', 'အောင်အောင်'), ('age', 25), ('job', 'Developer')]

4. .update() - Dictionary ပေါင်းမယ်

Dictionary တစ်ခုကို နောက်ထပ် Dictionary တစ်ခုနဲ့ ပေါင်းတယ်။

update_method.py
profile1 = {"name": "အောင်အောင်", "age": 25} profile2 = {"city": "Yangon", "job": "Developer"} # profile2 ကို profile1 ထဲ ပေါင်းထည့်မယ် profile1.update(profile2) print(profile1)

Output:

Output
{'name': 'အောင်အောင်', 'age': 25, 'city': 'Yangon', 'job': 'Developer'}

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

clear_method.py
my_profile = {"name": "အောင်အောင်", "age": 25} print(f"Before clear: {my_profile}") # Dictionary ထဲက items အကုန်လုံးကို ဖျက်မယ် my_profile.clear() print(f"After clear: {my_profile}")

Output:

Output
Before clear: {'name': 'အောင်အောင်', 'age': 25} After clear: {}

6. .setdefault() - Key မရှိရင် Default Value နဲ့ ထည့်မယ်

setdefault_method.py
my_profile = {"name": "အောင်အောင်"} # age key ရှိရင် value ပြန်ပေး၊ မရှိရင် 0 ထည့်ပြီး ပြန်ပေးမယ် age = my_profile.setdefault("age", 0) print(f"Age: {age}") print(f"Profile: {my_profile}") # name key က ရှိပြီးသား၊ value ကို ပြန်ပေးမယ် name = my_profile.setdefault("name", "Unknown") print(f"Name: {name}")

Output:

Output
Age: 0 Profile: {'name': 'အောင်အောင်', 'age': 0} Name: အောင်အောင်

Dictionary ကို Loop ပတ်မယ်

1. Keys ကို ပတ်တာ:

loop_keys.py
my_profile = { "name": "အောင်အောင်", "age": 25, "job": "Developer" } # Method 1: Default - Keys ကိုပဲ ပတ်တယ် for key in my_profile: print(key) print() # Method 2: .keys() ကို သုံးတာ for key in my_profile.keys(): print(key)

Output:

Output
name age job name age job

2. Values ကို ပတ်တာ:

loop_values.py
my_profile = { "name": "အောင်အောင်", "age": 25, "job": "Developer" } # Values ကိုပဲ ပတ်တယ် for value in my_profile.values(): print(value)

Output:

Output
အောင်အောင် 25 Developer

3. Key-Value Pairs ကို ပတ်တာ (အသုံးအများဆုံး):

.items() ကို ဘာလို့ အသုံးအဝင်ဆုံးလို့ ပြောတာလဲ? ဘာလို့လဲဆိုတော့... bro က Dictionary ကို ပတ်တော့မယ်ဆိုရင် Key ရော Value ရော တစ်ခါတည်း လိုချင်တာမျိုး အများကြီးကြုံလာမှာ။

loop_items.py
my_profile = { "name": "အောင်အောင်", "age": 25, "job": "Developer" } # Unpacking (Tuple မှာ သင်ခဲ့တာ) နဲ့ ပေါင်းသုံးတာ for key, value in my_profile.items(): print(f"သူ့ရဲ့ {key} က {value} ဖြစ်ပါတယ်")

Output:

Output
သူ့ရဲ့ name က အောင်အောင် ဖြစ်ပါတယ် သူ့ရဲ့ age က 25 ဖြစ်ပါတယ် သူ့ရဲ့ job က Developer ဖြစ်ပါတယ်

Dictionary တွေ ပေါင်းခြင်း

Method 1: .update() Method

merge_with_update.py
dict1 = {"a": 1, "b": 2} dict2 = {"c": 3, "d": 4} dict1.update(dict2) print(dict1)

Output:

Output
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Method 2: Dictionary Unpacking (** Operator)

merge_with_unpacking.py
dict1 = {"a": 1, "b": 2} dict2 = {"c": 3, "d": 4} # ** unpacking operator သုံးပြီး ပေါင်းမယ် merged = {**dict1, **dict2} print(merged) # မူရင်း dictionaries တွေ မပြောင်းဘူး print(f"dict1: {dict1}") print(f"dict2: {dict2}")

Output:

Output
{'a': 1, 'b': 2, 'c': 3, 'd': 4} dict1: {'a': 1, 'b': 2} dict2: {'c': 3, 'd': 4}

Dictionary Keys ရဲ့ စည်းကမ်း

Keys Requirements: Dictionary keys တွေမှာ အရေးကြီးတဲ့ စည်းကမ်း နှစ်ချက် ရှိတယ်။

1. Keys Must be Unique (Key တွေ ထပ်လို့ မရဘူး):

unique_keys.py
# တူညီတဲ့ key နှစ်ခု ရှိရင် နောက်ဆုံးက value ကိုပဲ ယူတယ် my_dict = { "name": "အောင်အောင်", "age": 25, "name": "မောင်မောင်" # ဒီ value ကို ယူမယ် } print(my_dict)

Output:

Output
{'name': 'မောင်မောင်', 'age': 25}

2. Keys Must be Immutable (Key တွေ Immutable ဖြစ်ရမယ်):

String, Number, Tuple တို့ကို Key အဖြစ် သုံးလို့ရတယ်။ List, Dictionary, Set တို့လို type တွေကိုတော့ Key အနေနဲ့ သုံးလို့ မရဘူး။

immutable_keys.py
# OK - String, Number, Tuple keys good_dict = { "name": "အောင်အောင်", # String key - OK 123: "number key", # Number key - OK (1, 2): "tuple key" # Tuple key - OK } print(good_dict) # Not OK - List key (mutable) try: bad_dict = {[1, 2]: "list key"} # TypeError! except TypeError as e: print(f"Error: {e}")

Output:

Output
{'name': 'အောင်အောင်', 123: 'number key', (1, 2): 'tuple key'} Error: unhashable type: 'list'

Nested Dictionaries (Dictionary ထဲက Dictionary)

Dictionary ထဲမှာ နောက်ထပ် Dictionary ထပ်ထည့်လို့ရတယ်။ ဒါက complex data structure တွေ ဆောက်ဖို့ အသုံးဝင်တယ်။ နောင်မှာ မကြာမကြာ တွေ့ရမယ်။

ဥပမာ: Students Database

nested_dict.py
students = { "student1": { "name": "အောင်အောင်", "age": 20, "grades": { "math": 85, "english": 90 } }, "student2": { "name": "မောင်မောင်", "age": 21, "grades": { "math": 78, "english": 88 } } } # Nested dictionary ကို access လုပ်မယ် print(students["student1"]["name"]) print(students["student1"]["grades"]["math"]) # Loop နဲ့ ပတ်မယ် for student_id, info in students.items(): print(f"\n{student_id}:") print(f" Name: {info['name']}") print(f" Age: {info['age']}") print(f" Math Grade: {info['grades']['math']}")

Output:

Output
အောင်အောင် 85 student1: Name: အောင်အောင် Age: 20 Math Grade: 85 student2: Name: မောင်မောင် Age: 21 Math Grade: 78

Dictionary Comprehension

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

အခြေခံ Dictionary Comprehension:

dict_comprehension.py
# ဂဏန်း နဲ့ သူ့ရဲ့ square ကို Dictionary လုပ်မယ် squares = {x: x**2 for x in range(6)} print(squares) # Condition နဲ့ even_squares = {x: x**2 for x in range(10) if x % 2 == 0} print(even_squares)

Output:

Output
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25} {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}

List ကနေ Dictionary ဆောက်တာ:

list_to_dict.py
fruits = ["apple", "banana", "orange"] # Fruit နာမည် နဲ့ သူ့ရဲ့ length ကို Dictionary လုပ်မယ် fruit_lengths = {fruit: len(fruit) for fruit in fruits} print(fruit_lengths)

Output:

Output
{'apple': 5, 'banana': 6, 'orange': 6}

Practical Examples

Example 1: User Profile

user_profile_complete.py
# User profile ဆောက်မယ် user = { "username": "aungaung123", "email": "aung@email.com", "age": 25, "is_active": True, "preferences": { "language": "Burmese", "theme": "dark" } } # Profile information ပြမယ် print("=== User Profile ===") print(f"Username: {user['username']}") print(f"Email: {user['email']}") print(f"Age: {user['age']}") print(f"Status: {'Active' if user['is_active'] else 'Inactive'}") print(f"Language: {user['preferences']['language']}")

Example 2: Student Records

student_records.py
# Student grades dictionary grades = { "အောင်အောင်": 85, "မောင်မောင်": 92, "ကျော်ကျော်": 78, "စိုးစိုး": 88 } # အမှတ် ပြမယ် print("=== Student Grades ===") for name, grade in grades.items(): print(f"{name}: {grade}") # ပျမ်းမျှ အမှတ် တွက်မယ် average = sum(grades.values()) / len(grades) print(f"\nပျမ်းမျှ အမှတ်: {average:.2f}") # အမြင့်ဆုံး အမှတ် ရှာမယ် top_student = max(grades, key=grades.get) print(f"Top student: {top_student} ({grades[top_student]})")

Output:

Output
=== Student Grades === အောင်အောင်: 85 မောင်မောင်: 92 ကျော်ကျော်: 78 စိုးစိုး: 88 ပျမ်းမျှ အမှတ်: 85.75 Top student: မောင်မောင် (92)

ဘယ်ချိန် Dictionary သုံးမလဲ?

Dictionary သုံးသင့်တဲ့ အချိန်: Dictionary က Key-Value structure လိုအပ်တဲ့ data တွေအတွက် အကောင်းဆုံးပဲ။

Dictionary သုံးသင့်တဲ့ အနေအထားတွေ:

  • Labeled Data: Data တွေကို နာမည် (label) နဲ့ သိမ်းချင်ရင် (ဥပမာ: profile, settings)
  • Fast Lookup: Key နဲ့ value ကို မြန်မြန် ရှာချင်ရင်
  • Structured Data: JSON လို structured data တွေ
  • Configuration: Settings, configurations တွေ
  • Database Records: Database records လို data တွေ

Dictionary မသုံးသင့်တဲ့ အခြေအနေ:

  • Ordered Collection: item တွေအစီစဉ်တကျဖြစ်ဖို့ အရေးကြီးရင် List သုံး
  • Simple List: Label မလိုဘဲ data တွေ သိမ်းရုံဆိုရင် List သုံး
  • Unique Items Only: Unique items ပဲ လိုအပ်ရင် Set သုံး
  • Immutable Collection: အပြောင်းလဲ မလုပ်စေချင်ရင် Tuple သုံး

Dictionary vs List - ဥပမာ:

dict_vs_list.py
# List - Index နဲ့ access (မရှင်း) person_list = ["အောင်အောင်", 25, "Developer"] print(person_list[0]) # ဘာလဲ? name? age? # Dictionary - Key နဲ့ access (ရှင်း) person_dict = { "name": "အောင်အောင်", "age": 25, "job": "Developer" } print(person_dict["name"]) # ရှင်းတယ် - name ကို ယူတယ်!

Real-world Use Cases

Dictionary တွေကို လက်တွေ့မှာ ဘယ်လို သုံးလဲ အတိုချုပ် ကြည့်ကြရအောင်:

  • Configuration Settings: App settings, user preferences တွေ သိမ်းတာ
  • JSON Data: APIs ကနေ လာတဲ့ JSON data တွေကို Python dictionary အဖြစ် သုံးတယ်
  • Counting Occurrences: စာလုံး ဘယ်နှခါ ထပ်လဲ ရေတွက်တာ
  • Caching: Data တွေကို မြန်မြန် access လုပ်ဖို့ cache လုပ်တာ
  • Database Records: Database records တွေကို represent လုပ်တာ

အနှစ်ချုပ်

Dictionary ဆိုတာ:

  • Key-Value Pairs structure (နာမည် နဲ့ တန်ဖိုး စုံတွဲ)
  • Curly braces {} နဲ့ ဆောက်တယ်
  • Keys က unique နဲ့ immutable ဖြစ်ရမယ်
  • Values ကတော့ ဘာမဆို ဖြစ်လို့ရတယ်

Accessing Data:

  • my_dict["key"] - Direct access (KeyError ဖြစ်နိုင်တယ်)
  • my_dict.get("key") - Safe access (None ပြန်ပေးတယ်)
  • my_dict.get("key", default) - Default value နဲ့

Methods:

  • .keys() - Key တွေ ဆွဲထုတ်
  • .values() - Value တွေ ဆွဲထုတ်
  • .items() - Key-Value pairs ဆွဲထုတ် (အသုံးများဆုံး)
  • .get() - Safe access
  • .pop() - ဖျက်ပြီး value ပြန်ယူ
  • .update() - Dictionary ပေါင်း
  • .clear() - အကုန် ရှင်း
  • .setdefault() - Default value နဲ့ ထည့်

Looping:

  • for key in my_dict: - Keys ကို ပတ်
  • for value in my_dict.values(): - Values ကို ပတ်
  • for key, value in my_dict.items(): - Both ကို ပတ် (အကောင်းဆုံး)

ဘာကြောင့် Dictionary အရေးကြီးလဲ?

  • Structured data သိမ်းဖို့ အကောင်းဆုံး
  • Key နဲ့ fast lookup လုပ်လို့ရတယ်
  • JSON, APIs, Configuration files အကုန် Dictionary structure ကို သုံးတယ်
  • Real-world applications မှာ အသုံးအများဆုံး data structure ပဲ
Okay bro။ Dictionary ကို ချုပ်ရရင် Key-Value Pair ကိုအခြေခံထားတဲ့ data structure။ အဘိဓာန် စာအုပ်လို၊ ဖုန်းစာအုပ်လို... နာမည်နဲ့ ခေါ်တာ။ Index နံပါတ်နဲ့ မဟုတ်ဘူး။ ဒါက Python မှာ အရေးအကြီးဆုံး Data Structure တစ်ခုပဲ။ ဒါကို ပိုင်သွားရင် bro Python မှာ ဆာကီးဖြစ်လာပြီ။

Quick Reference

dict_reference.py
# Creation my_dict = {"key": "value"} empty = {} from_dict = dict(key="value") # Accessing my_dict["key"] my_dict.get("key") my_dict.get("key", "default") # Adding/Modifying my_dict["new_key"] = "value" my_dict["existing_key"] = "new_value" # Deleting del my_dict["key"] my_dict.pop("key") # Checking "key" in my_dict # Methods my_dict.keys() my_dict.values() my_dict.items() my_dict.update(other_dict) my_dict.clear() my_dict.setdefault("key", "default") # Looping for key in my_dict: pass for value in my_dict.values(): pass for key, value in my_dict.items(): pass # Comprehension {k: v for k, v in ...} # Merging dict1.update(dict2) merged = {**dict1, **dict2}