Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - REMOVE FROM LIST


How to remove an element from the List by its name/value?


Let us say, we have a List that contains three names, 'Mohan', 'Kriti' and 'Salim'. And we want to remove 'Kriti' from the List.


It can be done with the 'remove( )' Function


Example :


x = ["Mohan", "Kriti", "Salim"]
x.remove("Kriti")
print(x) 


Output :



  ['Mohan', 'Salim']

So, in the above code we have created a 'List' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the List,


java_Collections

Next, we have used the 'remove( )' function that searches for the name 'Kriti' and removes it from the List.


x.remove("Kriti")

java_Collections

And we get the below output,


['Mohan', 'Salim']

How to remove an element from the List by its index/position?


Let us say, we have a List that contains three names, 'Mohan', 'Kriti' and 'Salim'. And we want to remove the element at index/position '2' from the List.


So, we can use 'del' keyword or the 'pop( )' function to remove an element from the List.


Let us see the example with 'del' keyword first.


Example :


x = ["Mohan", "Kriti", "Salim"]
del x[2]
print(x) 


Output :



  ['Mohan', 'Kriti']

So, in the above code we have created a 'List' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the List,


java_Collections

Next, we have used the 'del' keyword that searches for the element at index/position '2' and removes it from the List.


del x[2]

And as we can see, there is 'Salim' at index/position '2'. So 'Salim' is removed from the List.


java_Collections

And we get the below output,


['Mohan', 'Kriti']

Next, let us look at the same example using 'pop( )' Function.


Example :


x = ["Mohan", "Kriti", "Salim"]
x.pop(2)
print(x) 


Output :



  ['Mohan', 'Kriti']

So, in the above code we have created a 'List' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the List,


java_Collections

Next, we have used the 'pop( )' function that searches for the index/position '2' and removes it from the List.


x.pop(2)

And as we can see, there is 'Salim' at index/position '2'. So 'Salim' is removed from the List.


And we get the below output,


['Mohan', 'Kriti']

How to remove the last element from the List?


The 'pop( )' Function can be used to remove an item from the end of the List. If no index is specified at the parameter of 'pop(( )' Function then it removes the last element by default.


Example :


x = ["Mohan", "Kriti", "Salim"]
x.pop()
print(x) 


Output :



  ['Mohan', 'Kriti']

So, in the above code we have created a 'List' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the List,


java_Collections

Next, we have used the 'pop( )' function that searches for the lat value by default(As no index is specified) and removes it from the List.


x.pop()

And as we can see, there is 'Salim' at the end of the List. So 'Salim' is removed from the List.


java_Collections

And we get the below output,


['Mohan', 'Kriti']

How to remove all the elements from the List?


The 'clear( )' Function can be used to remove all the elements from the List.


Example :


x = ["Mohan", "Kriti", "Salim"]
x.clear()
print(x) 


Output :



  [ ]

So, in the above code we have created a 'List' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the List,


java_Collections

Next, we have used the 'clear( )' function that removes all the elements from the List making the List empty.


And we get an empty List as output,


[ ]