Python 목록 항목 제거
목록 항목 제거
목록에서 항목을 제거하는 방법에는 여러 가지가 있습니다.
예시
이 remove()
메서드는 지정된 항목을 제거합니다.
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
예시
이 pop()
메서드는 지정된 인덱스(또는 인덱스가 지정되지 않은 경우 마지막 항목)를 제거합니다.
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
예시
del
키워드는 지정된 인덱스를 제거합니다 .
thislist = ["apple", "banana", "cherry"]
del
thislist[0]
print(thislist)
예시
del
키워드는 목록을 완전히 삭제할 수도 있습니다 .
thislist = ["apple", "banana", "cherry"]
del
thislist
예시
이 clear()
메서드는 목록을 비웁니다.
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)