Skip to content

30 Days Of Python: Day 8 - Dictionaries

Twitter Follow Author: Asabeneh Yetayeh
Second Edition: July, 2021

<< Day 7 | Day 9 >>

30DaysOfPython

πŸ“˜ 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

λ”•μ…”λ„ˆλ¦¬λ₯Ό μ‚¬μš©ν•˜μ§€μ•ŠλŠ”λ‹€λ©΄ μ™„μ „νžˆ μ§€μšΈ 수 μžˆμŠ΅λ‹ˆλ‹€

# syntax
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
del dct

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

  1. dogλΌλŠ” μ΄λ¦„μ˜ 빈 λ”•μ…”λ„ˆλ¦¬λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€
  2. dog λ”•μ…”λ„ˆλ¦¬μ— name, color, breed, legs, age λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€
  3. student λ”•μ…”λ„ˆλ¦¬λ₯Ό μƒμ„±ν•˜κ³  first_name, last_name, gender, age, marital status, skills, country, city 와 address λ₯Ό ν‚€λ‘œ μΆ”κ°€ν•©λ‹ˆλ‹€
  4. student λ”•μ…”λ„ˆλ¦¬μ˜ 길이λ₯Ό μ–»μŠ΅λ‹ˆλ‹€
  5. skills 의 값을 μ–»κ³  μžλ£Œν˜•μ„ ν™•μΈν•©λ‹ˆλ‹€, list μ—¬μ•Ό ν•©λ‹ˆλ‹€
  6. ν•œκ°œλ‚˜ λ‘κ°œλ₯Ό μΆ”κ°€ν•΄ skills의 값을 μˆ˜μ •ν•©λ‹ˆλ‹€
  7. λ”•μ…”λ„ˆλ¦¬μ˜ ν‚€λ₯Ό 리슀트둜 μ–»μŠ΅λ‹ˆλ‹€
  8. λ”•μ…”λ„ˆλ¦¬μ˜ 값을 리슀트둜 μ–»μŠ΅λ‹ˆλ‹€
  9. items() λ©”μ„œλ“œλ₯Ό μ΄μš©ν•΄ νŠœν”Œμ˜ 리슀트둜 λ”•μ…”λ„ˆλ¦¬λ₯Ό λ°”κΏ‰λ‹ˆλ‹€
  10. λ”•μ…”λ„ˆλ¦¬μ˜ μ•„μ΄ν…œμ€‘ ν•˜λ‚˜λ₯Ό μ‚­μ œν•©λ‹ˆλ‹€
  11. λ”•μ…”λ„ˆλ¦¬ 쀑 ν•˜λ‚˜λ₯Ό μ‚­μ œν•©λ‹ˆλ‹€

πŸŽ‰ CONGRATULATIONS ! πŸŽ‰

<< Day 7 | Day 9 >>