- {key: value}
- 키는 해시 가능한 값만 사용 가능: 숫자, 문자열, 튜플 등 불변 자료형이 키로 사용된다
- 딕셔너리는 {}, 키로 접근 시 []
- 키 삭제 시 del 딕셔너리명[키 이름]
- keys(), values(), items()
- get() 예외처리가 적용된 함수, 없는 키로 접근 시 keyError 오류 발생
- update(), pop()
person = {"name": "Alice", "age": 25}
# 값 추가
person["city"] = "New York"
print(person) # {'name': 'Alice', 'age': 25, 'city': 'New York'}
# 특정 키 삭제
del person["city"]
print(person) # {'name': 'Alice', 'age': 25}
person = {"name": "Alice", "age": 25, "city": "New York"}
print(person.keys()) # dict_keys(['name', 'age', 'city'])
print(person.values()) # dict_values(['Alice', 25, 'New York'])
print(person.items()) # dict_items([('name', 'Alice'), ('age', 25), ('city', 'New York')])
person = {"name": "Alice", "age": 25}
print(person.get("name")) # Alice
print(person.get("city", "Unknown")) # Unknown
person = {"name": "Alice", "age": 25}
person.update({"city": "New York", "age": 26})
print(person) # {'name': 'Alice', 'age': 26, 'city': 'New York'}
person = {"name": "Alice", "age": 25}
age = person.pop("age")
print(age) # 25
print(person) # {'name': 'Alice'}
https://colab.research.google.com/drive/1PLNnaVc0lDg_NQ3UXD4vJhEjMtx9MCFQ
Google Colab Notebook
Run, share, and edit Python notebooks
colab.research.google.com
'python' 카테고리의 다른 글
[python] 파이썬 기본 (6) - 반복문 (0) | 2025.04.04 |
---|---|
[python] 파이썬 기본 (5) - 조건문 (0) | 2025.04.04 |
[python] 파이썬 기본 (3) - 리스트형 (0) | 2025.04.04 |
[python] 파이썬 기본 (2) - 문자형 (0) | 2025.04.04 |
[python] 파이썬 기본 (1) - 숫자형 (0) | 2025.04.04 |