Ads by ProfitSence
Close

Remove Item from List Python

Last Updated on Wednesday 5th Oct 2022

Python List Remove

  • In Python, there are many methods available on the list data type that help you remove an element from a given list.
  • The methods are remove(), pop() and clear()
			
					myList = [22 ,'Adam', 111, 1.5, 'Kate', 44, ['C', 'J']]

			
	

Remove Item from List Python

Python remove() method

  • Python remove() method is a built-in method available with the list.
  • It helps to remove the given very first element matching from the list.

Syntax

			
					list.remove(element)

			
	

The element that you want to remove from the list.

ReturnValue

There is no return value for this method.

Python Remove Item from List

			
					myList.remove(22) # remove 22 at the start.
print(myList)
myList.remove('Adam') # will remove the first Riya from the list
print(myList)
myList.remove(444)  #will throw an error
print(myList)

			
	
			
					# ['Adam', 111, 1.5, 'Kate', 44, ['C', 'J']]
# [111, 1.5, 'Kate', 44, ['C', 'J']]
Traceback (most recent call last):
  File "<string>", line 8, in <module>
ValueError: list.remove(x): x not in list

			
	

Python pop() method

The pop() method removes an element from the list based on the index given.

Syntax

			
					list.pop(index)

			
	

index: the pop() method has only one argument called index.

  • To remove an element from the list, you need to pass the index of the element. The index starts at 0. To get the first element from the list pass index as 0. To remove the last element, you can pass the index as -1.
  • The index argument is optional. If not passed, the default value is considered -1, and the last element from the list is returned.
  • If the index given is not present, or out of range, the pop() method throws an error saying IndexError: pop index.

ReturnValue

The pop() method will return the element removed based on the index given. The final list is also updated and will not have the element.

			
					name = myList.pop(2)
# remove first
print(f'{name} removed from  {myList}')

item = myList.pop()
# remove last
print(f'{item} removed from  {myList}')

item = myList.pop(15)
print(item)
print(myList)

			
	
			
					# 111 removed from  [22, 'Adam', 1.5, 'Kate', 44, ['C', 'J']]

# ['C', 'J'] removed from  [22, 'Adam', 1.5, 'Kate', 44]

Traceback (most recent call last):
  File "<string>", line 12, in <module>
IndexError: pop index out of range

			
	

Python List Remove

Python clear() method

The clear() method will remove all the elements present in the list.

Syntax

			
					list.clear()

			
	

Parameters

No parameters.

ReturnValue

  • There is no return value. The list() is emptied using clear()` method.
			
					name = myList.clear()

print(name)

print(myList)

			
	
			
					# None
# []

			
	

Python Delete List

Using del keyword

To remove an element from the list, you can use the del keyword followed by a list. You have to pass the index of the element to the list. The index starts at 0.

Syntax

			
					del list[index]

			
	

Delete List Python

  • You can also slice a range of elements from the list using the del keyword.
  • The start/stop index from the list can be given to del keyword, and the elements falling in that range will be removed.
  • The syntax is as follows:

Syntax

			
					del list[start:stop]

			
	
			
					del myList[0]
print("After removing first element", myList)

del myList[-1]
print("After removing last element", myList)

del myList[5]
print("After removing element from index:5", myList)

del myList[-2]
print("After removing last 2 elements", myList)

del myList[1:5]
print("After removing multiple elements from start:stop index (1:5)", myList)

del myList[4:]
print("To remove elements from index 4 till the end (4:)", myList)

			
	
			
					# After removing first element ['Adam', 111, 1.5, 'Kate', 44, ['C', 'J'], 'Alice', 23, 67]
# After removing last element ['Adam', 111, 1.5, 'Kate', 44, ['C', 'J'], 'Alice', 23]
# After removing element from index:5 ['Adam', 111, 1.5, 'Kate', 44, 'Alice', 23]
# After removing last 2 elements ['Adam', 111, 1.5, 'Kate', 44, 23]
# After removing multiple elements from start:stop index (1:5) ['Adam', 23]
# To remove elements from index 4 till the end (4:) ['Adam', 23]

			
	

Important built-in methods available on list to remove elements

  • remove() - It helps to remove the very first given element matching from the list.
  • pop() - The pop() method removes an element from the list based on the index given.
  • clear() - The clear() method will remove all the elements present in the list.