Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all but the first item in a list

Tags:

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".

like image 887
GowthamanSS Avatar asked Feb 07 '13 08:02

GowthamanSS


People also ask

How do I remove the first element from a list?

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 do you exclude an item from a list in Python?

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.


1 Answers

List.RemoveRange is what you're looking for:

if(list.Count > 1)     list.RemoveRange(1, list.Count - 1); 

Demo

like image 177
Tim Schmelter Avatar answered Oct 15 '22 01:10

Tim Schmelter