Ad code

Python - Dict/딕셔너리 키 삭제 하기

데이터를 처리하다보면, 불필요한 데이터를 지워야하는 경우가 종종 발생한다.

이때 사용할 수 있는 방법으로 바로 del operation를  통해 해당 키를 제거할 수 있다.


>>> test_dict = {"test":"test", "name":"han", "juseong":"hello"}

>>> test_dict

{'test': 'test', 'name': 'han', 'juseong': 'hello'}

>>> del test_dict["test"]

>>> test_dict

{'name': 'han', 'juseong': 'hello'}

그렇다면 하위에 하위키도 삭제가 가능할까?

하위키 역시 root 키를 지워서 전체를 지우거나 하위키에 존재하는 서브키만 지우는것도 가능하다.

>>> test_dict = {"test":"test", "name":{"last":"han","first":"juseong"}, "juseong":"hello"}

>>> test_dict

{'test': 'test', 'name': {'last': 'han', 'first': 'juseong'}, 'juseong': 'hello'}

>>> del test_dict['name']['first']

>>> test_dict

{'test': 'test', 'name': {'last': 'han'}, 'juseong': 'hello'}

댓글 쓰기

0 댓글