티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
Difference between del, remove, and pop on lists
리스트 del, remove 및 pop의 차이
문제 내용
Is there any difference between these three methods to remove an element from a list?
리스트에서 아이템을 제거하는 이 세 가지 방법 사이에 차이점이 있나요?
>>> a = [1, 2, 3]
>>> a.remove(2)
>>> a
[1, 3]
>>> a = [1, 2, 3]
>>> del a[1]
>>> a
[1, 3]
>>> a = [1, 2, 3]
>>> a.pop(1)
2
>>> a
[1, 3]
높은 점수를 받은 Solution
The effects of the three different methods to remove an element from a list:
리스트에서 요소를 제거하는 세 가지 다른 방법의 효과:
remove
removes the first matching value, not a specific index:
remove는 특정 인덱스가 아닌 첫 번째 일치 값을 제거합니다.
>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]
del
removes the item at a specific index:
del은 특정 인덱스에서 항목을 제거합니다.
>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
and pop
removes the item at a specific index and returns it.
특정 인덱스에서 아이템을 제거하고 반환합니다.
>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
Their error modes are different too:
오류 모드도 다릅니다.
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range
가장 최근 달린 Solution
Here is a detailed answer.
여기 자세한 답변이 있습니다.
del can be used for any class object whereas pop and remove and bounded to specific classes.
del은 임의의 클래스 객체에 사용될 수 있는 반면 pop과 remove는 특정 클래스로 제한된다.
For del
del의 경우
Here are some examples
여기 몇 가지 예가 있어요.
>>> a = 5
>>> b = "this is string"
>>> c = 1.432
>>> d = myClass()
>>> del c
>>> del a, b, d # we can use comma separated objects
We can override __del__
method in user-created classes.
사용자가 만든 클래스에서 __del_ 메서드를 재정의할 수 있습니다.
Specific uses with list
리스트와 함께 특정 용도
>>> a = [1, 4, 2, 4, 12, 3, 0]
>>> del a[4]
>>> a
[1, 4, 2, 4, 3, 0]
>>> del a[1: 3] # we can also use slicing for deleting range of indices
>>> a
[1, 4, 3, 0]
For pop
pop의 경우
pop
takes the index as a parameter and removes the element at that index
pop은 인덱스를 매개 변수로 사용하고 해당 인덱스에서 아이템을 제거합니다.
Unlike del
, pop
when called on list object returns the value at that index
del과 달리 pop은 목록 개체에서 호출될 때 해당 인덱스의 값을 반환합니다.
>>> a = [1, 5, 3, 4, 7, 8]
>>> a.pop(3) # Will return the value at index 3
4
>>> a
[1, 5, 3, 7, 8]
For remove
remove의 경우
remove takes the parameter value and remove that value from the list.
remove는 매개 변수 값을 가져와서 리스트에서 해당 값을 제거합니다.
If multiple values are present will remove the first occurrence
값이 여러 개 있으면 첫 번째 아이템이 제거됩니다.
Note
: Will throw ValueError if that value is not present
참고: 해당 값이 없으면 ValueError를 발생시킵니다.
>>> a = [1, 5, 3, 4, 2, 7, 5]
>>> a.remove(5) # removes first occurence of 5
>>> a
[1, 3, 4, 2, 7, 5]
>>> a.remove(5)
>>> a
[1, 3, 4, 2, 7]
Hope this answer is helpful.
이 답변이 도움이 되길 바랍니다.
출처 : https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists
'개발 > 파이썬' 카테고리의 다른 글
파일의 줄 내용 검색하여 바꾸기 (0) | 2022.12.14 |
---|---|
두 리스트 간의 차이로 새로운 리스트 만들기 (0) | 2022.12.14 |
파이썬에서 휠(wheel) 설치 오류 수정하기 (0) | 2022.12.13 |
리스트에 값이 있는지 확인하는 가장 빠른 방법 (0) | 2022.12.13 |
파일이 비어 있는지 확인하기 (0) | 2022.12.13 |