dic = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
dic['test'] = 'test'    # 딕셔너리 값 추가
dic[1] = 'test'
del dic[1] # key 값으로 삭제해야한다. ex) name, phone, birth
print(dic)          # {'name': 'pey', 'phone': '0119993323', 'birth': '1118', 'test': 'test'}
print(dic['test'])  # test

list_key = list(dic.keys())     # 딕셔너리의 key값을 리스트로 만들어 반환한다.
print(list_key)                 # ['name', 'phone', 'birth', 'test']
list_value = list(dic.values()) # 딕셔너리의 value값을 리스트로 만들어 반환한다.
print(list_value)               # ['pey', '0119993323', '1118', 'test']
list_item = list(dic.items())   # 딕셔너리를 (key, values) 튜플로 묶어 리스트로 반환
print(list_item)                # [('name', 'pey'), ('phone', '0119993323'), ('birth', '1118'), ('test', 'test')]
print(dic.get('name'))          # pey get(key) key로 value를 추출한다. dic.get(key) = dic[key]
print(dic.get('11', 'x'))       # dic(key, error) key 값이 없을 경우 error 쪽을 출력 
dic.clear()                     # 딕셔너리 다 지우기

'Python 공부(코테용)' 카테고리의 다른 글

조건문, 반복문  (0) 2022.01.17
파이썬 - 리스트  (0) 2022.01.17
파이썬 - 문자열  (0) 2022.01.17
파이썬 - 숫자형  (0) 2022.01.17

+ Recent posts