Python - 목록 항목 제거
지정된 항목 제거
remove()
메서드는 지정된 항목을 제거합니다 .
예시
"바나나" 제거:
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
지정된 인덱스 제거
메서드는 지정된 pop()
인덱스를 제거합니다.
예시
두 번째 항목을 제거합니다.
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
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)