Python - 사전 항목 제거
항목 제거
사전에서 항목을 제거하는 방법에는 여러 가지가 있습니다.
예시
이 pop()
메서드는 지정된 키 이름을 가진 항목을 제거합니다.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
예시
이 popitem()
메서드는 마지막으로 삽입된 항목을 제거합니다(3.7 이전 버전에서는 임의의 항목이 대신 제거됨).
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
예시
del
키워드는 지정된 키 이름을 가진 항목을 제거합니다 .
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
예시
del
키워드는 사전을 완전히 삭제할 수도 있습니다 .
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict"
no longer exists.
예시
이 clear()
메서드는 사전을 비웁니다.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)