파이썬 del

1 개요[ | ]

Python del Keyword, Python del
파이썬 del 키워드, 파이썬 del
  • 객체를 삭제하는 파이썬 키워드
  • 리스트에서 인덱스, 딕셔너리에서 키의 대상이 되는 객체를 삭제할 때 사용할 수 있다.
x = 1
del x
print( x )
→ 1행에서 x를 선언했지만, 2행에서 삭제(del)했으므로, 3행 print 문에서 변수를 찾지 못해 오류 발생
리스트
names = ['john', 'jane', 'chris', 'alex']
del names[0]
print( names )
# ['jane', 'chris', 'alex']
names = ['john', 'jane', 'chris', 'alex']
del names[:2]
print( names )
# ['chris', 'alex']
딕셔너리
fruits = { 'a' : 'apple', 'b' : 'banana', 'c': 'cherry' }
del fruits['b']
print( fruits )

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}