- π Day 8
- Dictionaries
- Creating a Dictionary
- Dictionary Length
- Accessing Dictionary Items
- Adding Items to a Dictionary
- Modifying Items in a Dictionary
- Checking Keys in a Dictionary
- Removing Key and Value Pairs from a Dictionary
- Changing Dictionary to a List of Items
- Clearing a Dictionary
- Deleting a Dictionary
- Copy a Dictionary
- Getting Dictionary Keys as a List
- Getting Dictionary Values as a List
- π» Exercises: Day 8
π Day 8
Dictionaries
Dictionaryλ μμκ° μλ μμ (λ³ν) κ°λ₯ν μ(ν€: κ°)μ μλ£νμ 컬λ μ μ λλ€.
Creating a Dictionary
Dictionaryλ₯Ό λ§λ€λ €λ©΄ μ€κ΄νΈ {} λλ dict() λ΄μ₯ ν¨μλ₯Ό μ¬μ©ν©λλ€.
# syntax
empty_dict = {}
# Dictionary with data values
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
Example:
person = {
'first_name':'Asabeneh',
'last_name':'Yetayeh',
'age':250,
'country':'Finland',
'is_marred':True,
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address':{
'street':'Space street',
'zipcode':'02210'
}
}
μλ¨μ Dictionaryλ κ°μ΄ μ΄λ€ μλ£νμΌ μλ μλ€λ κ²μ 보μ¬μ€λλ€:string, boolean, list, tuple, set λλ dictionary.
Dictionary Length
dictionary λ΄ 'key: value' μμ κ°μλ₯Ό νμΈν©λλ€.
# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
print(len(dct)) # 4
Example:
person = {
'first_name':'Asabeneh',
'last_name':'Yetayeh',
'age':250,
'country':'Finland',
'is_marred':True,
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address':{
'street':'Space street',
'zipcode':'02210'
}
}
print(len(person)) # 7
Accessing Dictionary Items
ν€μ μ΄λ¦μ ν΅ν΄ λμ λ리 μμ΄ν μ μ κ·Όν μ μμ΅λλ€.
# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
print(dct['key1']) # value1
print(dct['key4']) # value4
Example:
person = {
'first_name':'Asabeneh',
'last_name':'Yetayeh',
'age':250,
'country':'Finland',
'is_marred':True,
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address':{
'street':'Space street',
'zipcode':'02210'
}
}
print(person['first_name']) # Asabeneh
print(person['country']) # Finland
print(person['skills']) # ['JavaScript', 'React', 'Node', 'MongoDB', 'Python']
print(person['skills'][0]) # JavaScript
print(person['address']['street']) # Space street
print(person['city']) # Error
μ‘΄μ¬νμ§ μλ ν€μ μ΄λ¦μΌλ‘ μμ΄ν μ μ κ·Όν κ²½μ° μλ¬κ° λ°μν μ μμ΅λλ€. μ΄ μλ¬λ₯Ό νΌνκΈ°μν΄ μ°λ¦¬λ μ°μ ν€κ° μ‘΄μ¬νλμ§ νμΈν΄μΌν©λλ€. λλ get λ©μλλ₯Ό μ¬μ©ν μ μμ΅λλ€. get λ©μλλ ν€κ° μ‘΄μ¬νμ§ μμ κ²½μ°, NoneType object μλ£νμΈ Noneμ λ°νν©λλ€.
person = {
'first_name':'Asabeneh',
'last_name':'Yetayeh',
'age':250,
'country':'Finland',
'is_marred':True,
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address':{
'street':'Space street',
'zipcode':'02210'
}
}
print(person.get('first_name')) # Asabeneh
print(person.get('country')) # Finland
print(person.get('skills')) #['HTML','CSS','JavaScript', 'React', 'Node', 'MongoDB', 'Python']
print(person.get('city')) # None
Adding Items to a Dictionary
λμ λ리μ μλ‘μ΄ ν€μ κ°μ μμ μΆκ°ν μ μμ΅λλ€.
# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
dct['key5'] = 'value5'
Example:
person = {
'first_name':'Asabeneh',
'last_name':'Yetayeh',
'age':250,
'country':'Finland',
'is_marred':True,
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address':{
'street':'Space street',
'zipcode':'02210'
}
}
person['job_title'] = 'Instructor'
person['skills'].append('HTML')
print(person)
Modifying Items in a Dictionary
λμ λ리μ μμ΄ν μ μμ ν μ μμ΅λλ€
# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
dct['key1'] = 'value-one'
Example:
person = {
'first_name':'Asabeneh',
'last_name':'Yetayeh',
'age':250,
'country':'Finland',
'is_marred':True,
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address':{
'street':'Space street',
'zipcode':'02210'
}
}
person['first_name'] = 'Eyob'
person['age'] = 252
Checking Keys in a Dictionary
λμ λ리μ ν€κ° μ‘΄μ¬νλ μ§ νμΈνκΈ° μν΄ in μ°μ°μλ₯Ό μ¬μ©ν©λλ€
# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
print('key2' in dct) # True
print('key5' in dct) # False
Removing Key and Value Pairs from a Dictionary
- pop(key): νΉμ ν€ μ΄λ¦μ κ°μ§ μμ΄ν μ μμ ν©λλ€
- popitem(): λ§μ§λ§ μμ΄ν μ μμ ν©λλ€
- del: νΉμ ν€ μ΄λ¦μ κ°μ§ μμ΄ν μ μμ ν©λλ€
# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
dct.pop('key1') # removes key1 item
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
dct.popitem() # removes the last item
del dct['key2'] # removes key2 item
Example:
person = {
'first_name':'Asabeneh',
'last_name':'Yetayeh',
'age':250,
'country':'Finland',
'is_marred':True,
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address':{
'street':'Space street',
'zipcode':'02210'
}
}
person.pop('first_name') # Removes the firstname item
person.popitem() # Removes the address item
del person['is_married'] # Removes the is_married item
Changing Dictionary to a List of Items
items() λ©μλλ λμ λ리λ₯Ό ννμ 리μ€νΈλ‘ λ³νν©λλ€.
# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
print(dct.items()) # dict_items([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3'), ('key4', 'value4')])
Clearing a Dictionary
λμ λ리 λ΄μ μμ΄ν μ μνμ§ μλλ€λ©΄ clear() λ©μλλ₯Ό μ¬μ©ν΄ λΉμΈ μ μμ΅λλ€
# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
print(dct.clear()) # None
Deleting a Dictionary
λμ λ리λ₯Ό μ¬μ©νμ§μλλ€λ©΄ μμ ν μ§μΈ μ μμ΅λλ€
Copy a Dictionary
copy() λ©μλλ₯Ό ν΅ν΄ λμ λ리λ₯Ό 볡μ¬ν μ μμ΅λλ€. copyλ₯Ό μ¬μ©ν΄ μλ λμ λ리μ λ³νλ₯Ό λ§μ μ μμ΅λλ€.
# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
dct_copy = dct.copy() # {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
Getting Dictionary Keys as a List
keys() λ©μλλ νλμ λμ λ리μ λͺ¨λ ν€λ₯Ό 리μ€νΈλ‘ μ€λλ€.
# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
keys = dct.keys()
print(keys) # dict_keys(['key1', 'key2', 'key3', 'key4'])
Getting Dictionary Values as a List
values λ©μλλ νλμ λμ λ리μ λͺ¨λ κ°μ 리μ€νΈλ‘ μ€λλ€.
# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
values = dct.values()
print(values) # dict_values(['value1', 'value2', 'value3', 'value4'])
π λΉμ μ μ λ§ λλΌμμ. μ΄μ , μ¬λ¬λΆμ μ¬μ μ νμΌλ‘ μμ ν μΆ©μ λμ΄ μμ΅λλ€. μ¬λ¬λΆμ μ΄μ λ§ 8μΌμ§Έμ λμ μ λ§μ³€κ³ μλν¨μ ν₯ν΄ 8보 μ μ§νμ΅λλ€. μ΄μ μ¬λ¬λΆμ λμ κ·Όμ‘μ μν μ΄λμ νμΈμ.
π» Exercises: Day 8
- dogλΌλ μ΄λ¦μ λΉ λμ λ리λ₯Ό μμ±ν©λλ€
- dog λμ λ리μ name, color, breed, legs, age λ₯Ό μΆκ°ν©λλ€
- student λμ λ리λ₯Ό μμ±νκ³ first_name, last_name, gender, age, marital status, skills, country, city μ address λ₯Ό ν€λ‘ μΆκ°ν©λλ€
- student λμ λ리μ κΈΈμ΄λ₯Ό μ»μ΅λλ€
- skills μ κ°μ μ»κ³ μλ£νμ νμΈν©λλ€, list μ¬μΌ ν©λλ€
- νκ°λ λκ°λ₯Ό μΆκ°ν΄ skillsμ κ°μ μμ ν©λλ€
- λμ λ리μ ν€λ₯Ό 리μ€νΈλ‘ μ»μ΅λλ€
- λμ λ리μ κ°μ 리μ€νΈλ‘ μ»μ΅λλ€
- items() λ©μλλ₯Ό μ΄μ©ν΄ ννμ 리μ€νΈλ‘ λμ λ리λ₯Ό λ°κΏλλ€
- λμ λ리μ μμ΄ν μ€ νλλ₯Ό μμ ν©λλ€
- λμ λ리 μ€ νλλ₯Ό μμ ν©λλ€
π CONGRATULATIONS ! π