갬미의 성장일기

[Python] 딕셔너리 하나의 key에 여러개 value 추가하기 본문

Algorithm/Python 문법

[Python] 딕셔너리 하나의 key에 여러개 value 추가하기

갬미 2022. 1. 6. 10:45

딕셔너리 키에 value를 추가하고싶다면 value를 리스트로 주고 append 하면 된다

## dictionary 요소 수정, 추가하기

dictionary = { 'name' : '7D 건조 망고',
             'ingredient' : ['망고', '설탕', '메타중아황산나트륨', '치자황색소'],
             'origin' : '필리핀',
             'type' : '당절임'}
             
## 요소 추가하기
dictionary['ingredient'].append('꿀')
dictionary

list로 선언하지 않은 value에 그냥 append하면 오류남 (name, origin, type)

 

value 수정하기

## value 바꾸기
dictionary['name'] = '8D 건조 망고'
dictionary

'Algorithm > Python 문법' 카테고리의 다른 글

[Python] 진수 변환하기  (0) 2022.01.15
[Python] class  (0) 2022.01.07
[Python] 아스키코드 사용하기  (0) 2022.01.05
[Python] switch-case문 사용하기  (0) 2022.01.05
Comments