let us consider a list as below
list contains values as a,b,c,d
....
i need a query to just remove all the values in the list other than that "a".
The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.
How to Remove an Element from a List Using the remove() Method in Python. To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method. remove() will search the list to find it and remove it.
List.RemoveRange
is what you're looking for:
if(list.Count > 1) list.RemoveRange(1, list.Count - 1);
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With