Python list remove ()

Python Methods and Functions

Syntax :

 list_name.remove (obj) 

Parameters :

  obj  - object to be removed from the list 

Returns :

 The method does not return any value but removes the given object from the list. 

Note. It removes the first occurrence of an item from the list. 
Code # 1:

# Python3 program to demonstrate usage
# remove () method

 

 
# first occurrence 1 is removed from the list

list1 = [ 1 , 2 , 1 , 1 , 4 , 5

list1.remove ( 1

  print (list1) 

 
# removes & # 39; a & # 39; from list 2

list2 = [ 'a' , ' b' , 'c' , ' d'

list2.remove ( 'a'

print (list2)

Output:

 [2, 1, 1 , 4, 5] ['b',' c', 'd'] 

Error:

 It returns  ValueError  when the passed object is not present in the list 

Code # 2:

# Python3 program for error in
# remove () method

  

  
# removes & # 39; e & # 39; from list 2

list2 = [ 'a' , ' b' , 'c' , ' d'

list2.remove ( 'e'

print (list2)

Exit:

 Traceback (most recent call last): File "/home/e35b642d8d5c06d24e9b31c7e7b9a7fa.py", line 8, in list2.remove ('e') ValueError: list.remove (x): x not in list 

Practical Application:
Once you receive the list, remove all 1s from the list and print the list.

Code # 3:

# Python3 program for practical use
# remove 1 until all 1 are removed from the list

  

 

list1 = [ 1 , 2 , 3 , 4 , 1 , 1 , 1 , 4 , 5 ]

 
# loop until all 1s are deleted

while (list1.count ( 1 )):

  list1.remove ( 1

  

print (list1) 

Exit:

 [2, 3, 4, 4, 5 ]