John Winston
John Winston

Reputation: 25

How to replace a word to another word in a list python?

If I have this list:

['hello', 'world', 'goodbye', 'world', 'food']

Is it possible to replace every world to any other word without using any auto command, I am thinking about something like this:

if 'world' in list:
    'world' in list == 'peace'

Upvotes: 1

Views: 9527

Answers (9)

Pablo Vilas
Pablo Vilas

Reputation: 586

If you have unique values in your list:

my_list[my_list.index('old_word')]='new_word' 

Upvotes: 1

Oluwafemi Sule
Oluwafemi Sule

Reputation: 38952

You can map over each world and replace with a suitable word.

Using a lambda function,

words = map(lambda word: word.replace('world', 'peace') , l)

Upvotes: 0

shankarj67
shankarj67

Reputation: 1159

Iterate over list and replace "world" with "peace"

wordlist = ['hello', 'world', 'goodbye', 'world', 'food']


for i in range(0,len(wordlist)):
   if wordlist[i]=="world":
      wordlist[i]="peace"
print(wordlist) 

wordlist = ['hello', 'peace', 'goodbye', 'peace', 'food']  

Upvotes: 0

Sachin PC
Sachin PC

Reputation: 452

def customReplaceFunction(wordList, replacingWord):
    for i in range(len(wordList)):
        if wordList[i] == 'world':
            wordList[i] = replacingWord
    print wordList

Upvotes: 0

JacobIRR
JacobIRR

Reputation: 8946

This is an expanded view of what is happening in Zach's answer

word_list = ['hello', 'world', 'goodbye', 'world', 'food']
for ndx, word in enumerate(word_list):
    if word == 'world':
        word_list[ndx] = 'peace'
print word_list
# result: ['hello', 'peace', 'goodbye', 'peace', 'food']

Upvotes: 0

Yaman Jain
Yaman Jain

Reputation: 1251

Although there isn't any list.replace() method in the python standard library.

But this utility function may help you:

def replace(l, target_word, replaced_word):
    for index, element in enumerate(l):
        if element == target_word:
            l[index] = replaced_word

    return l

Upvotes: 0

pazt
pazt

Reputation: 11

I don't know if i understand your question, but it should work:

lst = ['hello', 'world', 'goodbye', 'world', 'food']

lst = [i.replace('world', 'peace') for i in lst]

print(lst2)

"If" statement isn't needed. Cause if 'world' not exist, it will just ignore it

Upvotes: 0

somil
somil

Reputation: 360

I don't think there is an out of the box solution e.g. something like my_list.replace() that you are looking for. Thus, the simple solution is just to iterate through the entire list (using enumerate to preserve the iterative variable).

Try this:

for index, elem in enumerate(my_list):
    if elem == "world":
        my_list[index] = "peace"

Upvotes: 0

Zach Gates
Zach Gates

Reputation: 4130

You can use a list comprehension with an if-else.

list_A = ['hello', 'world', 'goodbye', 'world']
list_B = [word if word != 'world' else 'friend' for word in list_A]

You now have a new list, list_B, where all instances of the word "world" have been replaced with "friend".

Upvotes: 4

Related Questions