티스토리 뷰

Python

딕셔너리형(dict)

홍삼어멈 2022. 1. 11. 23:58

중복을 허용하지 않고 순서가 없음. 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
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함