Replacing an element in a list with elements of another list without using dictionaries

I am developing a function in python. Here is my content:

list = ['cow','orange','mango']
to_replace = 'orange'
replace_with = ['banana','cream']

So I want my list to be the same after replacing

list = ['cow','banana','cream','mango']

I am using this function:

def replace_item(list, to_replace, replace_with):
    for n,i in enumerate(list):
      if i== to_replace:
         list[n]=replace_with
    return list

It displays the list as follows:

['cow', ['banana', 'cream'], 'mango']

So, how do I change this function to get the result below?

list = ['cow','banana','cream','mango']

NOTE. I found the answer here: Replacing a list item with the contents of another list, but I do not want to include dictionaries in it. I also want to change only my current function and keep it simple and straightforward.

+4
source share
5 answers

-, python ( list ls).

, , :

In [107]: from itertools import chain    
In [108]: ls = ['cow','orange','mango']
In [109]: to_replace = 'orange'
In [110]: replace_with = ['banana','cream']
In [112]: idx = ls.index(to_replace)  
In [116]: list(chain(ls[:idx], replace_with, ls[idx+1:]))
Out[116]: ['cow', 'banana', 'cream', 'mango']

python 3.5+ :

[*ls[:idx], *replace_with, *ls[idx+1:]]
+3

@TadhgMcDonald-Jensen iter_replace() (3,6 ):

lst = ['cow','orange','mango']
to_replace = 'orange'
replace_with = ['banana','cream']

def replace_item(lst, to_replace, replace_with):
    return sum((replace_with if i==to_replace else [i] for i in lst), [])

print replace_item(lst, to_replace, replace_with)
# ['cow', 'banana', 'cream', 'mango']

- itertools, (5.3 ):

import itertools
def replace_item(lst, to_replace, replace_with):
    return list(itertools.chain.from_iterable(
            replace_with if i==to_replace else [i] for i in lst
    ))

, (1,8 ):

def replace_item(lst, to_replace, replace_with):
    return [j for i in lst for j in (replace_with if i==to_replace else [i])]

, , (1,2 ):

def replace_item(lst, to_replace, replace_with):
    result = []
    for i in lst:
        if i == to_replace:
            result.extend(replace_with)
        else:
            result.append(i)
    return result

, , . , (python , , ).

+5

, , . for-loop.

, while , for:

>>> list_ = ['cow','orange','mango']
>>> to_replace = 'orange'
>>> replace_with = ['banana','cream']
>>> while True:
...     try:
...         i = list_.index(to_replace)
...     except ValueError:
...         break
...     else:
...         list_[i:i+1] = replace_with
...         
>>> list_
['cow', 'banana', 'cream', 'mango']
+5

, to_replace, :

def iter_replace(iterable, to_replace, replace_with):
    for item in iterable:
        if item == to_replace:
            yield from replace_with
        else:
            yield item

list(iter_replace(...)), , list.

ls = ['cow','orange','mango']
to_replace = 'orange'
replace_with = ['banana','cream']

print(list(iter_replace(ls,to_replace,replace_with)))

# ['cow', 'banana', 'cream', 'mango']
+3
source
def replace_item(list, to_replace, replace_with):
    index = list.index(to_replace) # the index where should be replaced
    list.pop(index) # pop it out
    for value_to_add in reversed(replace_with):
        list.insert(index, value_to_add) # insert the new value at the right place
    return list
-1
source

Source: https://habr.com/ru/post/1654292/


All Articles