2

Is there a fancy method for replacing a specific value of a list with another value?
Like a shortcut for this:

>>> l = list(range(10))
>>> replacing = 3
>>> l[l.index(replacing)] = 4
>>> l
[0, 1, 2, 4, 4, 5, 6, 7, 8, 9]

With the example I gave it's easy enough to do via the [l.index()], but when the list reference is a few dots away it starts getting ugly. It would be so much prettier to do something like this:

>>> some.thing.very.far.away.list = list(range(10))
>>> some.thing.very.far.away.list.replace(3, 4) 

Edit:
I forgot to say why.
I want to edit the same list, and only edit one of the values.
I'm actually kind of supprized that lists doesn't have a built-in method like list.replace(old, new[, max), considering that strings do and it seems like python has built-ins for just about everying.

cajomar
  • 428
  • 5
  • 9
  • You cann't define a variable `some.thing.very.far.away.list` like this? – Narendra Prasath Jun 23 '20 at 15:53
  • 1
    Note that your solution only treat the first occurrence of the item to replace – Paul Bombarde Jun 23 '20 at 15:57
  • 1
    Lists are mutable structures, so you can pass them to a function and the function can modify them. That way you don't have to repeat the verbose variable name multiple times. – Mark Ransom Jun 23 '20 at 15:58
  • @AnnZen I haven't accepted any answers because none of them quite give me the answer I was wanting (see my edit). Your answer was good, but I only wanted to replace _one_ element. Sorry I forgot to specify in the original question. – cajomar Jul 09 '20 at 15:26
  • @cajomar I updated. – Red Jul 09 '20 at 15:33
  • I just want to add that your list is probably ill-defined. You have a list in which the index always equals the value. That is kind of useless and might give you solutions which won't work on any other list in the world – Qohelet Nov 26 '21 at 10:19
  • @Qohelet That was am arbitrary example, not a real use case. – cajomar Nov 27 '21 at 14:53

4 Answers4

0

Build a new list with a list comprehension:

new_items = [4 if x==3 else x for x in l]

You can modify the original list in-place if you want, but it doesn't actually save time:

items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for index, item in enumerate(items):
    if (item ==3 2):
        items[index] = 4
Sanket Singh
  • 1,246
  • 1
  • 9
  • 26
0

you can assign "some.thing.very.far.away.list" to a temporary variable and apply replace function

some.thing.very.far.away.list  = temp
something.very.far.away.list = temp.replace(x,y)
0

This can be a trick:

First define a dictionary with values you want to replace, than use a list comprehension using dictionary get with a default value. You are passing el both as a key of the dictionary and as default value. If the key is found the corresponding value will be replaced otherwise the default value itself.

>>> l = [0, 1, 2, 4, 4, 5, 6, 7, 8, 9]
>>> rpl = {1: 23, 6: 12}
>>> [rpl.get(el, el) for el in l]
[0, 23, 2, 4, 4, 5, 12, 7, 8, 9]
piertoni
  • 1,933
  • 1
  • 18
  • 30
0

You can use map method to iterate and replace certain value.
syntax : map(function, iterable, ...)
The returned value from map() (map object) can then be passed to functions like list() (to create a list), set() (to create a set) and so on.

l = list(range(10))
replacing = 3
l =  list(map(lambda x: 4 if x == replacing else x, l)) # iterating over list l and replacing certain value using map method and converting map object in to another list.
print(l)

output = [0, 1, 2, 4, 4, 5, 6, 7, 8, 9]

it takes two parameter function and iterable. map() passes each item of the iterable to this function.
where lambda function takes argument x from sequence (l), assign 4 if x is equal to 4 . to know more about map method Python map()

shivam sharma
  • 121
  • 1
  • 4