dictionary

- 키 : 값 쌍에 데이터 값을 저장하는 데 사용

- 변경 가능하며 중복을 허용하지 않음

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)

항목 변경

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["year"] = 2018 # 1964을 2018으로 변경
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.update({"year": 2020})

항목 추가

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["color"] = "red"
print(thisdict)
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.update({"color": "red"})

항목제거

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.pop("model") #pop을 이용해 키값이 model 제거
print(thisdict)
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.popitem() #popitem으로 마지막으로 삽입된 항목 제거
print(thisdict)
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
del thisdict["model"]
print(thisdict)

- del 는 dictionary 삭제 가능

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer exists.

출처

https://www.w3schools.com/python/python_dictionaries.asp

'Python' 카테고리의 다른 글

함수  (0) 2021.06.17
반복문, 조건문  (0) 2021.06.17
Set  (0) 2021.06.17
Tuple  (0) 2021.06.17
List  (0) 2021.06.17

+ Recent posts