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.