요소 제거 Python LIST [지우기, 팝, 제거, 삭제]

Python 목록 데이터 유형은 다양한 데이터 유형의 항목을 정렬된 순서로 저장하는 데 도움이 됩니다. 데이터는 대괄호([]) 안에 작성되고 값은 쉼표(,)로 구분됩니다.

In Python, 주어진 목록에서 요소를 제거하는 데 도움이 되는 목록 데이터 유형에 사용할 수 있는 많은 메서드가 있습니다. 방법은 다음과 같습니다 제거(), 팝()맑은() .

목록 방법 외에도 다음을 사용할 수도 있습니다. 목록에서 항목을 제거하는 키워드입니다.

목록의 예

my_list = ['Guru', 50, 11.50, 'Siya', 50, ['A', 'B', 'C']]

인덱스는 0부터 시작합니다. 목록에서: my_list at

0th 인덱스에는 'Guru'라는 문자열이 있습니다.

  • index: 1 에서는 정수인 숫자 50을 얻게 됩니다.
  • index:2 에서 부동 숫자 11.50을 얻게 됩니다.
  • index:3에 'Siya'라는 문자열이 있습니다.
  • index:4에서 숫자 50이 중복된 것을 볼 수 있습니다.
  • index:5에서 A, B, C 값이 포함된 목록을 얻게 됩니다.

Python remove() 메서드

Python 제거() 메소드는 목록과 함께 사용할 수 있는 내장 메소드입니다. 목록에서 일치하는 첫 번째 요소를 제거하는 데 도움이 됩니다.
구문 :

list.remove(element)

목록에서 제거하려는 요소입니다.

반환값

이 메서드에는 반환 값이 없습니다.

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()

매개 변수 :

매개변수가 없습니다.

반환값:

반환 값이 없습니다. list()는 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() 메소드의 경우 제거할 첫 번째 요소를 전달해야 하며 인덱스(즉, 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']

제품 개요

In Python, 주어진 목록에서 요소를 제거하는 데 도움이 되는 목록 데이터 유형에 사용할 수 있는 많은 메서드가 있습니다. 방법은 다음과 같습니다 제거(), 팝()분명한().

요소를 제거하기 위해 목록에서 사용할 수 있는 중요한 내장 메소드

방법 기술설명
없애다() 목록에서 일치하는 첫 번째 요소를 제거하는 데 도움이 됩니다.
팝() pop() 메소드는 주어진 인덱스를 기반으로 목록에서 요소를 제거합니다.
맑은() Clear() 메소드는 목록에 있는 모든 요소를 ​​제거합니다.

이 게시물을 요약하면 다음과 같습니다.