요소 제거 Python LIST [지우기, 팝, 제거, 삭제]
⚡ 스마트 요약
요소 제거 Python list는 remove(), pop(), clear()라는 세 가지 내장 메서드와 del 키워드를 사용하며, 각 메서드는 값으로 항목을 선택하거나, 인덱스로 항목을 선택하거나, 전체 리스트를 제자리에서 지울 수 있습니다.

Python 리스트 데이터 타입은 서로 다른 데이터 타입의 항목들을 순서대로 저장하는 데 유용합니다. 데이터는 대괄호([]) 안에 작성하고, 값들은 쉼표(,)로 구분합니다.
In Python, 주어진 목록에서 요소를 제거하는 데 도움이 되는 목록 데이터 유형에 사용할 수 있는 많은 메서드가 있습니다. 방법은 다음과 같습니다 제거(), 팝() 맑은().
목록 방법 외에도 다음을 사용할 수도 있습니다. 델 목록에서 항목을 제거하는 키워드입니다.
목록의 예
my_list = ['Guru', 50, 11.50, 'Siya', 50, ['A', 'B', 'C']]
인덱스는 0부터 시작합니다. 리스트 my_list에서 0번째 인덱스에는 문자열 '이 있습니다.Guru'.
- 인덱스 1에서는 정수인 50이라는 숫자가 나옵니다.
- index:2 에서 부동 숫자 11.50을 얻게 됩니다.
- index:3에 'Siya'라는 문자열이 있습니다.
- index:4에서 숫자 50이 중복된 것을 볼 수 있습니다.
- index:5에서 A, B, C 값이 포함된 목록을 얻게 됩니다.
Python remove() 메서드
`remove()` 메서드는 리스트에 내장된 메서드입니다. 이 메서드는 리스트에서 일치하는 첫 번째 요소를 제거하는 데 사용됩니다.
구문 :
list.remove(element)
목록에서 제거하려는 요소입니다.
반환 값
이 메서드에는 반환 값이 없습니다.
remove() 메서드 사용 팁
remove() 메서드를 사용할 때 기억해야 할 중요한 사항은 다음과 같습니다.
- 목록에 중복된 요소가 있으면 해당 요소와 일치하는 첫 번째 요소가 목록에서 제거됩니다.
- 주어진 요소가 목록에 없으면 해당 요소가 목록에 없다는 오류가 발생합니다.
- remove() 메서드는 아무 값도 반환하지 않습니다.
- remove() 메서드는 값을 인수로 받으므로, 값은 올바른 데이터 형식으로 전달되어야 합니다.
예시: remove() 메서드를 사용하여 리스트에서 요소를 제거하는 방법
제가 가지고 있는 샘플 목록은 다음과 같습니다.
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']
이 리스트에는 문자열과 숫자 데이터 형식의 요소가 있습니다. 또한 숫자 12와 문자열 "Riya"처럼 중복된 요소가 있습니다.
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] my_list.remove(12) # it will remove the element 12 at the start. print(my_list) my_list.remove('Riya') # will remove the first Riya from the list print(my_list) my_list.remove(100) #will throw an error print(my_list)
출력:
['Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] ['Siya', 'Tiya', 14, 12, 'Riya'] Traceback (most recent calllast): File "display.py", line 9, in <module> my_list.remove(100) ValueError: list.remove(x): x not in the list
Python 팝() 메소드
pop() 메소드는 주어진 인덱스를 기반으로 목록에서 요소를 제거합니다.
통사론
list.pop(index)
index: pop() 메서드에는 index라는 인수가 하나만 있습니다.
- 리스트에서 요소를 제거하려면 해당 요소의 인덱스를 전달해야 합니다. 인덱스는 0부터 시작합니다. 리스트의 첫 번째 요소를 가져오려면 인덱스에 0을 전달하고, 마지막 요소를 제거하려면 인덱스에 -1을 전달하면 됩니다.
- index 인수는 선택 사항입니다. 전달되지 않으면 기본값은 -1로 간주되고 목록의 마지막 요소가 반환됩니다.
- 주어진 인덱스가 존재하지 않거나 범위를 벗어나면 pop() 메소드는 다음과 같은 오류를 발생시킵니다. IndexError: 팝 인덱스.
반환 값 :
pop() 메소드는 주어진 인덱스에 따라 제거된 요소를 반환합니다. 최종 목록도 업데이트되며 해당 요소가 없습니다.
예시: pop() 메서드를 사용하여 리스트에서 요소를 제거하는 방법
예제에서 사용할 리스트는 my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']입니다.
다음을 기반으로 pop() 메서드를 사용하여 요소를 제거해 보겠습니다.
- 인덱스를 주어서
- 인덱스 없음
- 범위를 벗어난 인덱스를 전달했습니다.
여기서는 제거 중입니다. 티야 목록에서 가져옵니다. 인덱스는 0부터 시작하므로 인덱스는 다음과 같습니다. 티야 2입니다.
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] #By passing index as 2 to remove Tiya name = my_list.pop(2) print(name) print(my_list) #pop() method without index – returns the last element item = my_list.pop() print(item) print(my_list) #passing index out of range item = my_list.pop(15) print(item) print(my_list)
출력:
Tiya [12, 'Siya', 14, 'Riya', 12, 'Riya'] Riya [12, 'Siya', 14, 'Riya', 12] Traceback (most recent calllast): File "display.py", line 14, in <module> item = my_list.pop(15) IndexError: popindex out of range
Python 클리어() 메소드
Clear() 메소드는 목록에 있는 모든 요소를 제거합니다.
구문 :
list.clear()
매개 변수 :
매개변수가 없습니다.
반환 값 :
반환 값이 없습니다. clear() 메서드를 사용하여 리스트를 비웁니다.
예시: clear() 메서드를 사용하여 리스트에서 모든 요소를 제거하는 방법
Clear() 메소드는 주어진 목록을 비울 것입니다. 아래 예에서clear()의 작동을 살펴보겠습니다.
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] #Using clear() method element = my_list.clear() print(element) print(my_list)
출력:
None
[]
del 키워드 사용
목록에서 요소를 제거하려면 다음을 사용할 수 있습니다. 델 키워드 뒤에 목록이 옵니다. 요소의 인덱스를 목록에 전달해야 합니다. 인덱스는 0부터 시작합니다.
구문 :
del list[index]
또한 다음을 사용하여 목록에서 요소 범위를 분할할 수도 있습니다. 델 `del` 키워드를 사용하면 리스트의 시작/종료 인덱스를 지정할 수 있으며, 해당 범위에 속하는 요소들이 제거됩니다. 구문은 다음과 같습니다.
구문 :
del list[start:stop]
다음은 리스트에서 첫 번째 요소, 마지막 요소 또는 여러 요소를 제거하는 방법을 보여주는 예입니다. 델.
my_list = list(range(15)) print("The Original list is ", my_list) #To remove the firstelement del my_list[0] print("After removing first element", my_list) #To remove last element del my_list[-1] print("After removing last element", my_list) #To remove element for given index : for example index:5 del my_list[5] print("After removing element from index:5", my_list) #To remove last 2 elements from the list del my_list[-2] print("After removing last 2 elements", my_list) #To remove multiple elements delmy_list[1:5] print("After removing multiple elements from start:stop index (1:5)", my_list) #To remove multiple elements del my_list[4:] print("To remove elements from index 4 till the end (4:)", my_list)
출력:
The Originallist is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] After removing first element [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] After removing last element [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] After removing element from index:5 [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13] After removing last 2 elements [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13] After removing multiple elements from start:stop index (1:5) [1, 7, 8, 9, 10, 11, 13] To remove elements from index 4 till the end (4:) [1, 7, 8, 9]
목록에서 첫 번째 요소를 어떻게 제거합니까?
다음과 같은 목록 방법을 사용할 수 있습니다. 제거(), 팝() 리스트에서 첫 번째 요소를 제거하려면, remove() 메서드에는 제거할 첫 번째 요소를 전달해야 하고, pop() 메서드에는 인덱스(예: 0)를 전달해야 합니다.
당신은 또한 사용할 수 있습니다 델 목록에서 첫 번째 요소를 제거하는 키워드입니다.
아래 예시는 remove(), pop(), del 함수를 사용하여 리스트에서 첫 번째 요소를 제거하는 방법을 보여줍니다.
my_list1 = ['A', 'B', 'C', 'D', 'E', 'F'] print("The Originallist is ", my_list1) #Using remove() to remove first element my_list1.remove('A') print("Using remove(), the final list is ", my_list1) my_list1 = ['A', 'B', 'C', 'D', 'E', 'F'] print("The Originallist is ", my_list1) #Using pop() to remove the first element element = my_list1.pop(0) print("The first element removed from my_list1 is ", element) print("Using pop(), the final list is ", my_list1) #Using del to remove the first element my_list2 = ['A', 'B', 'C', 'D', 'E', 'F'] del my_list2[0] print("Using del, the final list is ", my_list2)
출력:
The Originallist is ['A', 'B', 'C', 'D', 'E', 'F'] Using remove(), the final list is ['B', 'C', 'D', 'E', 'F'] The Originallist is ['A', 'B', 'C', 'D', 'E', 'F'] The first element removed from my_list1 is A Using pop(), the final list is ['B', 'C', 'D', 'E', 'F'] Using del, the final list is ['B', 'C', 'D', 'E', 'F']
목록에서 여러 요소를 제거하려면 어떻게 해야 합니까? Python?
리스트의 remove() 및 pop() 메서드는 단일 요소를 제거하기 위한 것입니다. 여러 요소를 제거하려면 다른 방법을 사용해야 합니다. 델 예어.
리스트 ['A', 'B', 'C', 'D', 'E', 'F']에서 B, C, D를 제거하고 싶습니다. 아래 예시는 해당 기능을 활용하는 방법을 보여줍니다. 델 요소를 제거하는 키워드입니다.
#Using del to remove the multiple elements from list my_list2 = ['A', 'B', 'C', 'D', 'E', 'F'] print("Originallist is ", my_list2) del my_list2[1:4] print("Using del, the final list is ", my_list2)
출력:
Originallist is ['A', 'B', 'C', 'D', 'E', 'F'] Using del, the final list is ['A', 'E', 'F']
인덱스를 사용하여 목록에서 요소를 제거하는 방법은 무엇입니까? Python?
인덱스를 기준으로 요소를 제거하려면 리스트의 `pop()` 메서드를 사용할 수 있습니다. 이 메서드를 사용하더라도 마찬가지입니다. 델 키워드는 특정 인덱스의 요소를 제거하는 데 도움이 됩니다.
#Using del to remove the multiple elements from list my_list1 = ['A', 'B', 'C', 'D', 'E', 'F'] print("Originallist is ", my_list1) element = my_list1.pop(2) print("Element removed for index: 2 is ", element) print("Using pop, the final list is ", my_list1) #Using del to remove the multiple elements from list my_list2 = ['A', 'B', 'C', 'D', 'E', 'F'] print("Originallist is ", my_list2) del my_list2[2] print("Using del, the final list is ", my_list2)
출력:
Originallist is ['A', 'B', 'C', 'D', 'E', 'F'] Element removed for index: 2 is C Using pop, the final list is ['A', 'B', 'D', 'E', 'F'] Originallist is ['A', 'B', 'C', 'D', 'E', 'F'] Using del, the final list is ['A', 'B', 'D', 'E', 'F']
