Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace value in list at same collection location [duplicate]

Tags:

c#

How do I replace a value in a collection list at the same location?

0 = cat 1 = dog 2 = bird 

replace 2 with snail?

like image 421
jpavlov Avatar asked Apr 21 '11 17:04

jpavlov


People also ask

How do you replace multiple values in a list Python?

Replace Multiple Values in a Python List. There may be many times when you want to replace not just a single item, but multiple items. This can be done quite simply using the for loop method shown earlier.

How do you find and replace in a list Python?

Replace a specific string in a list. If you want to replace the string of elements of a list, use the string method replace() for each element with the list comprehension. If there is no string to be replaced, applying replace() will not change it, so you don't need to select an element with if condition .

How do I replace a list item with another list item?

Use Lambda to find the index in the List and use this index to replace the list item. This asks only once for the index. Your approach uses Contains first which needs to loop all items (in the worst case), then you're using IndexOf which needs to enumerate the items again .

How to replace a particular value in a Python list?

If we want to replace a list item at a particular index, we can simply directly assign new values to those indices. In the next section, you’ll learn how to replace a particular value in a Python list using a for loop. Want to learn how to use the Python zip () function to iterate over two lists?

How to replace values in a list using for loop?

We can use for loop to iterate over the list and replace values in the list. Suppose we want to replace ‘Hardik’ and ‘Pant’ from the list with ‘Shardul’ and ‘Ishan’. We first find values in the list using for loop and if condition and then replace it with the new value. We can also use a while loop to replace values in the list.

How do you find and replace multiple values in Excel?

Find and replace multiple values with nested SUBSTITUTE The easiest way to find and replace multiple entries in Excel is by using the SUBSTITUTE function. The formula's logic is very simple: you write a few individual functions to replace an old value with a new one.


2 Answers

Do you mean:

yourCollection[2] = "Snail"; 
like image 101
Chuck Callebs Avatar answered Sep 27 '22 16:09

Chuck Callebs


In a bigger List<T> collection, you would like to find index to replace...with:

 var i = animals.FindIndex(x => x == "Dog");  animals[i] = "Snail"; 
like image 28
pungggi Avatar answered Sep 27 '22 18:09

pungggi