🖥

[Python] 리스트에서 값 삭제하기 (pop(), remove(), del, clear 차이, 시간복잡도)

망록 2022. 6. 19.

list 삭제 관련 메소드 비교

 

 

pop()

안에 인덱스값을 안 넣어주면 마지막 값을 삭제해줌

해당 값을 리턴해줄 수 있음 

시간복잡도 O(1)

 

pop(n)

인덱스를 특정하여 삭제 가능

시간복잡도 O(N)

 

remove(n)

특정한 값을 삭제함

리턴값 없음

시간복잡도 O(N)

 

clear()

리스트를 비워줌 

시간복잡도 O(N)

arr = [i*2 for i in range(10)]
print(arr)

print(arr.pop())        #마지막 원소 리턴
print(arr)

print(arr.pop(0))       #0번째 원소 리턴
print(arr)

print(arr.pop(0))       #0번째 원소 리턴 
print(arr)

print(arr.remove(12))   #리턴값 없음 
print(arr)

print(arr.clear())    #리턴값 없음
print(arr)

 

 

실행결과

 

 

https://wiki.python.org/moin/TimeComplexity

 

TimeComplexity - Python Wiki

This page documents the time-complexity (aka "Big O" or "Big Oh") of various operations in current CPython. Other Python implementations (or older or still-under development versions of CPython) may have slightly different performance characteristics. Howe

wiki.python.org

 

댓글