티스토리 뷰
중복을 허용하지 않고 순서가 없음. key와 value의 세트로 되어있다. {Key : Value, ...}
선언:
sample_dict = {'일' : 'one', '이' : 'two', '삼' : 'three'}
접근:
sample_dict['이']
# 'two'
키만 가져오기
sample_dict.keys()
# dict_keys(['일', '이', '삼'])
list(sample_dict.keys())
# ['일', '이', '삼']
값만 가져오기
sample_dict.values()
# dict_values(['one', 'two', 'three'])
list(sample_dict.values())
# ['one', 'two', 'three']
키, 값을 모두 가져오기
sample_dict.items()
# dict_items([('일', 'one'), ('이', 'two'), ('삼', 'three')])
list(sample_dict.items())
# [('일', 'one'), ('이', 'two'), ('삼', 'three')]
요소 추가하기
sample_dict.update({'사' : 'four'})
# {'일' : 'one', '이' : 'two', '삼' : 'three', '사' : 'four'}
요소 빼기
sample_dict.pop('삼')
# {'일' : 'one', '이' : 'two', '사' : 'four'}
요소 삭제하기
del sample_dict['이']
'Python' 카테고리의 다른 글
List / Tuple / Set 구분 (0) | 2022.01.11 |
---|
댓글