Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding one item from list (by Index), and take all others

There is a List<int> containing some set of numbers. Randomly I select an index, which will be processed separately (call it master). Now, I want to exclude this particular index, and get all other elements of List (call them slave).

var items = new List<int> { 55, 66, 77, 88, 99 };
int MasterIndex = new Random().Next(0, items .Count);

var master = items.Skip(MasterIndex).First();

// How to get the other items into another List<int> now? 
/*  -- items.Join;
    -- items.Select;
    -- items.Except */

Join, Select, Except - any of them, and how?

EDIT: Cannot remove any item from the original list, otherwise I have to keep two lists.

like image 981
Ajay Avatar asked Dec 31 '14 09:12

Ajay


People also ask

How do you exclude items from a list in Python?

In Python, use list methods clear() , pop() , and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.

How do you remove an element from a specific index in Python?

You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output. You can also use negative index values.

How to pop last element from a list?

Using del The del operator deletes the element at the specified index location from the list. To delete the last element, we can use the negative index -1. The use of the negative index allows us to delete the last element, even without calculating the length of the list.

How to remove the last value from a list Python?

How do you remove the last element of a list in Python? The method pop() can be used to remove and return the last value from the list or the given index value. If the index is not given, then the last element is popped out and removed.


3 Answers

Use Where:-

var result = numbers.Where((v, i) => i != MasterIndex).ToList(); 

Working Fiddle.

like image 192
Rahul Singh Avatar answered Sep 23 '22 15:09

Rahul Singh


If performance is an issue you may prefer to use the List.CopyTo method like this.

List<T> RemoveOneItem1<T>(List<T> list, int index) {     var listCount = list.Count;      // Create an array to store the data.     var result = new T[listCount - 1];      // Copy element before the index.     list.CopyTo(0, result, 0, index);      // Copy element after the index.     list.CopyTo(index + 1, result, index, listCount - 1 - index);      return new List<T>(result); } 

This implementation is almost 3 times faster than @RahulSingh answer.

like image 37
Orace Avatar answered Sep 21 '22 15:09

Orace


You could remove Master item from the list,

List<int> newList = items.RemoveAt(MasterIndex);

RemoveAt() removes the item from the original list, So it's not necessary to assign the collection to a new list. After calling RemoveAt(), items.Contains(MasterItem) would return false.

like image 22
Kurubaran Avatar answered Sep 22 '22 15:09

Kurubaran