
컬λ μ νμ μ λ§μ§λ§ μλ£ν λμ λ리(dictionary)μ λν΄ μμ보μ.
νμ΄μ¬μ λμ λ리λ ν€-κ° μμ μ μ₯νλ μμκ° μλ λ³κ²½κ°λ₯ν(mutable) 컬λ μ μ΄λ€. ν€λ μΈνΈλ‘ μ΄λ£¨μ΄μ Έ μκΈ° λλ¬Έμ μ€λ³΅λ μ μμ§λ§, κ°μ μ€λ³΅μ΄ κ°λ₯νλ€.
dict1 = {}
print(dict1)
print(type(dict1))
> {}
> <class 'dict'>
dict2 = {1:'μ 민콩', 2:'μ ν콩', 3:'μν콩', 4:'μ λΉμ½©'}
print(dict2)
> {1: 'μ 민콩', 2: 'μ ν콩', 3: 'μν콩', 4: 'μ λΉμ½©'}
λμ λ리[ν€]μ ννλ‘ ν΄λΉ ν€μ κ°μ μ μ μλ€.
print(dict2[2])
print(dict2[4])
> μ ν콩
> μ λΉμ½©
dict3 = {'no':1, 'userid':'yoomin', 'name':'μ 민콩', 'hp':'010-1234-5678'}
print(dict3)
print(dict3['userid'])
print(dict3['name'])
> {'no': 1, 'userid': 'yoomin', 'name': 'μ 민콩', 'hp': '010-1234-5678'}
> yoomin
> μ 민콩
λμ λ리[μλ‘μ΄ ν€] = μλ‘μ΄ κ° μ ννλ‘ κ°μ μλ‘ μΆκ°ν μ μλ€. []μμ κΈ°μ‘΄μ ν€λ₯Ό λ£μΌλ©΄ ν€μ κ°μ λ³κ²½ν μ μλ€.
dict4 = {1:'μ 민콩'}
dict4[100] = 'μ λΉμ½©'
dict4[50] = 'μ ν콩'
dict4[100] = 'μν콩'
print(dict4)
> {1: 'μ 민콩', 100: 'μν콩', 50: 'μ ν콩'}
del ν¨μλ₯Ό ν΅ν΄ ν€λ₯Ό μ κ±°ν μ μλ€.
del dict4[100]
print(dict4)
> {1: 'μ 민콩', 50: 'μ ν콩'}
λμ λ리μ ν€λ λ³κ²½λΆκ°λ₯ν(immutable) νμ μ΄μ¬μΌ νλ€. μλ₯Ό λ€μ΄ λ¬Έμμ΄, μ μ, ννμ λμ λ리μ ν€λ‘ μ¬μ©ν μ μμ§λ§, 리μ€νΈλ λμ λ리μ ν€λ‘ μ¬μ©ν μ μλ€. νμ§λ§ λμ λ리μ κ°μ μ΄λ€ νμ μ΄λ μκ΄μλ€.
dict3 = {'no':1, 'userid':'yoomin', 'name':'μ 민콩', 'hp':'010-1234-5678'}
print(dict3)
dict3['no'] = 10 # κ° λ³κ²½
print(dict3)
dict3['gender'] = 'f' # ν€-κ° μΆκ°
print(dict3)
dict3['score'] = [90, 100, 60] # κ°μ 리μ€νΈ ννλ‘λ μ μ₯ κ°λ₯
print(dict3)
# dict3[[1, 2, 3]] = ['νλ', 'λ', 'μ
']
# TypeError: unhashable type: 'list'
# ν€μλ 리μ€νΈ μ¬μ© λΆκ°
dict3[(1, 2, 3)] = ['νλ', 'λ', 'μ
'] # ν€μ ννμ μ¬μ© κ°λ₯
print(dict3)
dict3['fruit'] = {'μ¬κ³Ό':'π', 'λΈκΈ°':'π', 'μλ°':'π'} # λμ
λ리μ λμ
λ리 μ μ₯ κ°λ₯
print(dict3)
> {'no': 1, 'userid': 'yoomin', 'name': 'μ 민콩', 'hp': '010-1234-5678'}
> {'no': 10, 'userid': 'yoomin', 'name': 'μ 민콩', 'hp': '010-1234-5678'}
> {'no': 10, 'userid': 'yoomin', 'name': 'μ 민콩', 'hp': '010-1234-5678', 'gender': 'f'}
> {'no': 10, 'userid': ' yoomin', 'name': 'μ 민콩', 'hp': '010-1234-5678', 'gender': 'f', 'score': [90, 100, 60]}
> {'no': 10, 'userid': 'yoomin', 'name': 'μ 민콩', 'hp': '010-1234-5678', 'gender': 'f', 'score': [90, 100, 60], (1, 2, 3): ['νλ', 'λ', 'μ
']}
> {'no': 10, 'userid': 'yoomin', 'name': 'μ 민콩', 'hp': '010-1234-5678', 'gender': 'f', 'score': [90, 100, 60], (1, 2, 3): ['νλ', 'λ', 'μ
'], 'fruit': > {'μ¬κ³Ό': 'π', 'λΈκΈ°': 'π', 'μλ°': 'π'}}
dict3 = {'no':1, 'userid':'yoomin', 'name':'μ 민콩', 'hp':'010-1234-5678'}
print(len(dict3)) # ν€-κ°μ΄ λͺ κ° λ€μ΄μλμ§
> 4
λμ λ리μ λͺ¨λ ν€λ₯Ό λ°ννλ€.
print(dict3.keys())
> dict_keys(['no', 'userid', 'name', 'hp'])
λμ λ리μ λͺ¨λ κ°μ λ°ννλ€.
print(dict3.values())
> dict_values([1, 'yoomin', 'μ 민콩', '010-1234-5678'])
λμ λ리μ λͺ¨λ ν€-κ°μ ννλ‘ λ°ννλ€
print(dict3.items())
> dict_items([('no', 1), ('userid', 'yoomin'), ('name', 'μ 민콩'), ('hp', '010-1234-5678')])
νΉμ ν€μ λν κ°μ λ°ννλ€, λ§μ½ ν€κ° λμ λ리μ μμΌλ©΄ Noneλ₯Ό λ°ννλ€.
dict3 = {'no':1, 'userid':'yoomin', 'name':'μ 민콩', 'hp':'010-1234-5678'}
print(dict3['userid'])
> yoomin
# print(dict3['gender'])
# KeyError: 'gender'
# μλ ν€ λΆλ₯΄λ©΄ μ€λ₯
print(dict3.get('userid'))
print(dict3.get('gender')) # None
print(dict3.get('age', 'λμ΄λ₯Ό μ μ μμ')) # Noneμ λμ ν΄μ μΆλ ₯ν λ¬Έμ₯
print(dict3.get('userid', 'μμ΄λλ₯Ό μ μ μμ'))
> yoomin
> None
> λμ΄λ₯Ό μ μ μμ
> yoomin
νΉμ ν€μ λν κ°μ μ κ±°νκ³ κ·Έ κ°μ λ°ννλ€. ν€κ° μλ€λ©΄ μλ¬λ₯Ό λ°ννλ€.
dict3 = {'no':1, 'userid':'yoomin', 'name':'μ 민콩', 'hp':'010-1234-5678'}
temp = dict3.pop('hp')
print(dict3)
print(temp)
> {'no': 1, 'userid': 'yoomin', 'name': 'μ 민콩'}
> 010-1234-5678
λμ λ리μ νΉμ ν€κ° μλμ§ νμΈνλ€. λ©μλλ μλλ€.
dict3 = {'no':1, 'userid':'yoomin', 'name':'μ 민콩', 'hp':'010-1234-5678'}
print('hp' in dict3)
print('gender' in dict3)
> True
> False