Dictionaries
Introduction
ဒီတစ်ခါ Dictionaries အကြောင်းကို ပြောပြပါမယ်။ ဒီကောင်က Python မှာ အရေးကြီးတဲ့ နှလုံးသားလေးပေါ့။ List, Tuple, Set တို့နဲ့ လုံးဝ မတူတဲ့ အထာ နဲ့ လာတာ။
List တို့က item ယူချင်ရင် [0], [1] ဆိုတဲ့ Index နံပါတ် နဲ့ ယူရတယ်။ ဘယ်နှခုမြောက် ဆိုတာ သိရတယ်။
Dictionary က အဲ့လို နံပါတ်နဲ့ မဟုတ်ဘူး။ သူက နာမည် (Key) နဲ့ ခေါ်တာ။
Dictionary ဆိုတာ ဘာလဲပေါ့?
Dictionary ကို ရှင်းပြရမယ်ဆိုရင်... သူက အဘိဓာန် စာအုပ်ကြီး (ဒါမှမဟုတ် ဖုန်းစာအုပ် Phone Book) နဲ့ တူတယ်။
- Key (သော့): ဒါက bro ရှာချင်တဲ့ စကားလုံး (ဥပမာ: "Apple")
- Value (တန်ဖိုး): ဒါက အဲ့ဒီစကားလုံးရဲ့ အဓိပ္ပာယ် (ဥပမာ: "ပန်းသီး")
Key တစ်ခုမှာ Value တစ်ခု... စုံတွဲလိုက် ရှိတယ်။ ဒါကို Key-Value Pair လို့ခေါ်တယ်။
Key/Value Pairs
Dictionary မှာ data တွေကို Key-Value Pair အဖြစ် သိမ်းတယ်။
# အဘိဓာန် ဥပမာ
# 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 ရယ်) စုံတွဲတွေ ထည့်ပေးရတယ်။
{} က Empty Dictionary ပဲ၊ Empty Set မဟုတ်ဘူး။ Empty Set လိုချင်ရင် set() သုံးရမယ်နော် မရော နဲ့။
ဥပမာ: bro တစ်ယောက်ရဲ့ Profile ဆောက်ကြည့်မယ်
my_profile = {
"name": "အောင်အောင်",
"age": 25,
"job": "Developer",
"is_cool": True,
"skills": ["Python", "Java", "SQL"]
}
print(my_profile)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 နည်းလမ်းများ:
# Empty Dictionary
empty_dict = {}
print(type(empty_dict)) # <class 'dict'>
# dict() constructor သုံးတာ
person = dict(name="မောင်မောင်", age=30, city="Yangon")
print(person)Output:
<class 'dict'>
{'name': 'မောင်မောင်', 'age': 30, 'city': 'Yangon'}Data ယူတဲ့ ပုံစံသစ်
List မှာတုန်းက my_list[0] ဆိုပြီး index နဲ့ ယူတယ်။ Dictionary မှာတော့ my_dict["key_name"] key နဲ့ ယူတယ်။
Square Brackets [] နဲ့ Access လုပ်တာ:
my_profile = {
"name": "အောင်အောင်",
"age": 25,
"job": "Developer"
}
# အောင်အောင် ရဲ့ အလုပ်ကို သိချင်ရင်
print(my_profile["job"])
# သူ့အသက်ကို သိချင်ရင်
print(my_profile["age"])Output:
Developer
25KeyError - မရှိတဲ့ Key ကို Access လုပ်ရင်:
my_profile = {"name": "အောင်အောင်", "age": 25}
# မရှိတဲ့ key ကို access လုပ်ကြည့်မယ်
try:
print(my_profile["city"]) # KeyError!
except KeyError:
print("Error: 'city' key က Dictionary မှာ မရှိဘူး!")Output:
Error: 'city' key က Dictionary မှာ မရှိဘူး!.get() Method - ဘေးကင်းတဲ့ Access:
.get() method က KeyError မတက်ဘူး။ Key မရှိရင် None (ဒါမှမဟုတ် default value) ပြန်ပေးတယ်။
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:
City: None
City: Unknown
Name: အောင်အောင်[] vs .get() - ဘယ်အချိန် ဘာသုံးမလဲ?
- [] - Key ရှိမှာ သေချာရင် သုံးပေါ့
- .get() - Key ရှိ/မရှိ မသေချာရင်၊ default value လိုအပ်တဲ့ခါ
Data ထည့်တာ၊ ပြင်တာ
List ထက်တောင် လွယ်သေးတယ်။
Value ကို ပြင်တာ:
my_profile = {
"name": "အောင်အောင်",
"age": 25,
"job": "Developer"
}
# အသက် ကို ပြင်မယ်
my_profile["age"] = 26 # 25 ကနေ 26 ဖြစ်သွားပြီ
print(my_profile)Output:
{'name': 'အောင်အောင်', 'age': 26, 'job': 'Developer'}Key အသစ် ထပ်ထည့်တာ:
my_profile = {
"name": "အောင်အောင်",
"age": 25
}
# မြို့ (city) ဆိုတဲ့ Key အသစ် ထပ်ထည့်မယ် (မရှိဘူး အရင်က)
my_profile["city"] = "Yangon"
# အခု ပြန်ထုတ်ကြည့်
print(my_profile)Output:
{'name': 'အောင်အောင်', 'age': 25, 'city': 'Yangon'}Item တွေ ဖျက်မယ်
1. del keyword - Key ကို ဖျက်မယ်
my_profile = {
"name": "အောင်အောင်",
"age": 25,
"city": "Yangon"
}
# city key ကို ဖျက်မယ်
del my_profile["city"]
print(my_profile)Output:
{'name': 'အောင်အောင်', 'age': 25}2. .pop() Method - ဖျက်ပြီး Value ကို ပြန်ယူမယ်
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:
Removed city: Yangon
Profile: {'name': 'အောင်အောင်', 'age': 25}
Country: MyanmarKey ရှိ/မရှိ စစ်မယ်
Method 1: in operator သုံးတာ
my_profile = {
"name": "အောင်အောင်",
"age": 25
}
# Key ရှိ/မရှိ စစ်မယ်
if "name" in my_profile:
print("name key ရှိတယ်")
if "city" not in my_profile:
print("city key မရှိဘူး")Output:
name key ရှိတယ်
city key မရှိဘူးMethod 2: .get() နဲ့ None စစ်တာ
my_profile = {"name": "အောင်အောင်", "age": 25}
# .get() က None ပြန်ပေးရင် key မရှိဘူး
if my_profile.get("city") is None:
print("city key မရှိဘူး")Output:
city key မရှိဘူးDictionary Methods
Dictionary နဲ့ အလုပ်လုပ်ဖို့ အသုံးတည့်တဲ့ Methods တွေ ရှိတယ်။
1. .keys() - Key တွေ ချည်းပဲ ဆွဲထုတ်မယ်
ဒါက Dictionary ထဲက Key (သော့) တွေကိုပဲ ဆွဲထုတ်ပေးတယ်။ ဖုန်းစာအုပ်ထဲက နာမည် တွေချည်းပဲ အကုန်ပေး ဆိုသလိုပဲ။
my_profile = {
"name": "အောင်အောင်",
"age": 25,
"job": "Developer",
"city": "Yangon"
}
# Key တွေ အကုန် ဆွဲထုတ်မယ်
all_keys = my_profile.keys()
print(all_keys)
print(list(all_keys)) # List အဖြစ် ပြောင်းလို့ရတယ်Output:
dict_keys(['name', 'age', 'job', 'city'])
['name', 'age', 'job', 'city']2. .values() - Value တွေ ချည်းပဲ ဆွဲထုတ်မယ်
ဒါက Dictionary ထဲက Value (တန်ဖိုး) တွေကိုပဲ ဆွဲထုတ်မယ်။ ဖုန်းစာအုပ်ထဲက ဖုန်းနံပါတ် တွေချည်းပဲ အကုန်ပေး ဆိုသလိုပဲ။
my_profile = {
"name": "အောင်အောင်",
"age": 25,
"job": "Developer"
}
# Value တွေ အကုန် ဆွဲထုတ်မယ်
all_values = my_profile.values()
print(all_values)
print(list(all_values))Output:
dict_values(['အောင်အောင်', 25, 'Developer'])
['အောင်အောင်', 25, 'Developer']3. .items() - စုံတွဲလိုက် အကုန် ဆွဲထုတ်မယ်
ဒါက အသုံးအဝင်ဆုံးပဲ။ သူက Key ရော Value ရော စုံတွဲလိုက် (Tuple အနေနဲ့) ဆွဲထုတ်ပေးတယ်။ ဖုန်းစာအုပ်ထဲက နာမည်ရော ဖုန်းနံပါတ်ရော စုံတွဲလိုက် အကုန်ပေးလို့ ပြောလိုက်သလိုပေါ့။
my_profile = {
"name": "အောင်အောင်",
"age": 25,
"job": "Developer"
}
# Key-Value pairs အကုန် ဆွဲထုတ်မယ်
all_items = my_profile.items()
print(all_items)
print(list(all_items))Output:
dict_items([('name', 'အောင်အောင်'), ('age', 25), ('job', 'Developer')])
[('name', 'အောင်အောင်'), ('age', 25), ('job', 'Developer')]4. .update() - Dictionary ပေါင်းမယ်
Dictionary တစ်ခုကို နောက်ထပ် Dictionary တစ်ခုနဲ့ ပေါင်းတယ်။
profile1 = {"name": "အောင်အောင်", "age": 25}
profile2 = {"city": "Yangon", "job": "Developer"}
# profile2 ကို profile1 ထဲ ပေါင်းထည့်မယ်
profile1.update(profile2)
print(profile1)Output:
{'name': 'အောင်အောင်', 'age': 25, 'city': 'Yangon', 'job': 'Developer'}5. .clear() - Dictionary ကို အကုန် ရှင်းမယ်
my_profile = {"name": "အောင်အောင်", "age": 25}
print(f"Before clear: {my_profile}")
# Dictionary ထဲက items အကုန်လုံးကို ဖျက်မယ်
my_profile.clear()
print(f"After clear: {my_profile}")Output:
Before clear: {'name': 'အောင်အောင်', 'age': 25}
After clear: {}6. .setdefault() - Key မရှိရင် Default Value နဲ့ ထည့်မယ်
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:
Age: 0
Profile: {'name': 'အောင်အောင်', 'age': 0}
Name: အောင်အောင်Dictionary ကို Loop ပတ်မယ်
1. Keys ကို ပတ်တာ:
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:
name
age
job
name
age
job2. Values ကို ပတ်တာ:
my_profile = {
"name": "အောင်အောင်",
"age": 25,
"job": "Developer"
}
# Values ကိုပဲ ပတ်တယ်
for value in my_profile.values():
print(value)Output:
အောင်အောင်
25
Developer3. Key-Value Pairs ကို ပတ်တာ (အသုံးအများဆုံး):
.items() ကို ဘာလို့ အသုံးအဝင်ဆုံးလို့ ပြောတာလဲ? ဘာလို့လဲဆိုတော့... bro က Dictionary ကို ပတ်တော့မယ်ဆိုရင် Key ရော Value ရော တစ်ခါတည်း လိုချင်တာမျိုး အများကြီးကြုံလာမှာ။
my_profile = {
"name": "အောင်အောင်",
"age": 25,
"job": "Developer"
}
# Unpacking (Tuple မှာ သင်ခဲ့တာ) နဲ့ ပေါင်းသုံးတာ
for key, value in my_profile.items():
print(f"သူ့ရဲ့ {key} က {value} ဖြစ်ပါတယ်")Output:
သူ့ရဲ့ name က အောင်အောင် ဖြစ်ပါတယ်
သူ့ရဲ့ age က 25 ဖြစ်ပါတယ်
သူ့ရဲ့ job က Developer ဖြစ်ပါတယ်Dictionary တွေ ပေါင်းခြင်း
Method 1: .update() Method
dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "d": 4}
dict1.update(dict2)
print(dict1)Output:
{'a': 1, 'b': 2, 'c': 3, 'd': 4}Method 2: Dictionary Unpacking (** Operator)
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:
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
dict1: {'a': 1, 'b': 2}
dict2: {'c': 3, 'd': 4}Dictionary Keys ရဲ့ စည်းကမ်း
1. Keys Must be Unique (Key တွေ ထပ်လို့ မရဘူး):
# တူညီတဲ့ key နှစ်ခု ရှိရင် နောက်ဆုံးက value ကိုပဲ ယူတယ်
my_dict = {
"name": "အောင်အောင်",
"age": 25,
"name": "မောင်မောင်" # ဒီ value ကို ယူမယ်
}
print(my_dict)Output:
{'name': 'မောင်မောင်', 'age': 25}2. Keys Must be Immutable (Key တွေ Immutable ဖြစ်ရမယ်):
String, Number, Tuple တို့ကို Key အဖြစ် သုံးလို့ရတယ်။ List, Dictionary, Set တို့လို type တွေကိုတော့ Key အနေနဲ့ သုံးလို့ မရဘူး။
# 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:
{'name': 'အောင်အောင်', 123: 'number key', (1, 2): 'tuple key'}
Error: unhashable type: 'list'Nested Dictionaries (Dictionary ထဲက Dictionary)
Dictionary ထဲမှာ နောက်ထပ် Dictionary ထပ်ထည့်လို့ရတယ်။ ဒါက complex data structure တွေ ဆောက်ဖို့ အသုံးဝင်တယ်။ နောင်မှာ မကြာမကြာ တွေ့ရမယ်။
ဥပမာ: Students Database
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:
အောင်အောင်
85
student1:
Name: အောင်အောင်
Age: 20
Math Grade: 85
student2:
Name: မောင်မောင်
Age: 21
Math Grade: 78Dictionary Comprehension
List Comprehension လို Dictionary Comprehension လည်း ရှိတယ်။ Dictionary ကို တစ်ကြောင်းတည်းနဲ့ ရေးလို့ရတယ်။
အခြေခံ Dictionary Comprehension:
# ဂဏန်း နဲ့ သူ့ရဲ့ 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:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}List ကနေ Dictionary ဆောက်တာ:
fruits = ["apple", "banana", "orange"]
# Fruit နာမည် နဲ့ သူ့ရဲ့ length ကို Dictionary လုပ်မယ်
fruit_lengths = {fruit: len(fruit) for fruit in fruits}
print(fruit_lengths)Output:
{'apple': 5, 'banana': 6, 'orange': 6}Practical Examples
Example 1: User Profile
# 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 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:
=== Student Grades ===
အောင်အောင်: 85
မောင်မောင်: 92
ကျော်ကျော်: 78
စိုးစိုး: 88
ပျမ်းမျှ အမှတ်: 85.75
Top student: မောင်မောင် (92)ဘယ်ချိန် Dictionary သုံးမလဲ?
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 - ဥပမာ:
# 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 ပဲ
Quick Reference
# 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}