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".

Answer from Zach Gates on Stack Overflow
๐ŸŒ
Esri Community
community.esri.com โ€บ t5 โ€บ python-questions โ€บ using-replace-to-change-words-in-a-list โ€บ td-p โ€บ 435437
Solved: Using .replace to change words in a list - Esri Community
December 11, 2021 - The string.replace() function does not change the string in place, it returns a new string with the replaced words. ... feet_list = ["1000 feet", "23908 feet", "1200000000 feet"] meter_list = [] for area in feet_list: s = area.replace("feet", "meters") meter_list.append(s) print(meter_list) ...
Discussions

pandas - How to replace words in list with Python - Stack Overflow
I am trying to replace a certain set of words in a list with words from a different list. Check "s" If words in "invalid_list" are in "s" it should be replaced with x... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How Do I Replace Words In a String With Items In a List In Python - Stack Overflow
The string method "replace" can replace a given substring in a string by another one. You can also tell it to do it only once. Then you have to wrap this in a for-loop to replace one word after another. ... Let's try not to use Python built-in list as the variable name. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to replace a word found in a string with what is in the list in Python - Stack Overflow
My string is below: word = "Continue: Lifetime Benefits in Running, Volume 1, Issue 1, February 2018" My list is: italic_list = ['Continue', ': Lifetime Benefits in Running', ' February 2018'] I ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
text - Replace all words from word list with another string in python - Stack Overflow
I have a user entered string and I want to search it and replace any occurrences of a list of words with my replacement string. import re prohibitedWords = ["MVGame","Kappa","DatSheffy","DansGame"," More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_replace.asp
Python String replace() Method
Remove List Duplicates Reverse a String Add Two Numbers ยท Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ยท โฎ String Methods ยท Replace the word "bananas": txt = "I like bananas" x = txt.replace("bananas", "apples") print(x) Try it Yourself ยป ยท
Find elsewhere
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Extract and Replace Elements That Meet the Conditions of a List of Strings in Python | note.nkmk.me
May 19, 2023 - To replace the whole element containing a specific string, use the in operator to extract it and apply conditional expressions (ternary operator), formatted as X if condition else Y. ... Use conditional expressions for the expression part of ...
Top answer
1 of 4
41

You can do that with a single call to sub:

big_regex = re.compile('|'.join(map(re.escape, prohibitedWords)))
the_message = big_regex.sub("repl-string", str(word[1]))

Example:

>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile('|'.join(map(re.escape, prohibitedWords)))
>>> the_message = big_regex.sub("<replaced>", 'this message contains Some really Random Words')
>>> the_message
'this message contains <replaced> really <replaced> <replaced>'

Note that using str.replace may lead to subtle bugs:

>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
...     text = text.replace(word, 'swords')
... 
>>> text
'a sample message with sswords swords'

while using re.sub gives the correct result:

>>> big_regex = re.compile('|'.join(map(re.escape, words)))
>>> big_regex.sub("swords", 'a sample message with random words')
'a sample message with swords swords'

As thg435 points out, if you want to replace words and not every substring you can add the word boundaries to the regex:

big_regex = re.compile(r'\b%s\b' % r'\b|\b'.join(map(re.escape, words)))

this would replace 'random' in 'random words' but not in 'pseudorandom words'.

2 of 4
6

try this:

prohibitedWords = ["MVGame","Kappa","DatSheffy","DansGame","BrainSlug","SwiftRage","Kreygasm","ArsonNoSexy","GingerPower","Poooound","TooSpicy"]

themessage = str(word[1])    
for word in prohibitedwords:
    themessage =  themessage.replace(word, "(I'm an idiot)")

print themessage
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-replace-values-in-a-list-in-python
Replace Values in a List in Python - GeeksforGeeks
It works when the index of the element to be replaced is already known. ... This method checks every element in the list and replaces only those values that satisfy a given condition, while rebuilding the list in a single expression using list comprehension.
Published: January 12, 2026
๐ŸŒ
pythontutorials
pythontutorials.net โ€บ blog โ€บ replace-all-words-from-word-list-with-another-string-in-python
How to Replace All Words from a List with a String in Python: Fixing For Loop and re.sub Issues โ€” pythontutorials.net
Replacing multiple words in Python requires avoiding naive pitfalls like partial matches and unescaped regex characters. Use: Fixed For Loop: For small lists/texts, sorted by word length (descending).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-replace-substring-in-list-of-strings
Replace substring in list of strings - Python - GeeksforGeeks
July 11, 2025 - ... a = ["hello world", "world of code", "worldwide"] old_substring = "world" new_substring = "universe" res = [s.replace(old_substring, new_substring) for s in a] print(res) ... The replace() function replaces all occurrences of "world" with ...
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python: replace item in list (6 different ways)
Python: Replace Item in List (6 Different Ways) โ€ข datagy
August 12, 2022 - In the next section, youโ€™ll learn how to 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. Letโ€™s take a look at an example where we want to replace all known typos in a list with the word typo.
๐ŸŒ
stataiml
stataiml.com โ€บ posts โ€บ 35_find_replace_string_python_list
Find and Replace Values in List in Python - stataiml
May 3, 2024 - You can find and replace string values in a list using a list comprehension or map() function in Python. new_list = [s.replace('old_string', 'new_string') for s in input_list] new_list = map(lambda s: str.replace(s, 'old_string', 'new_string'), ...
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
How to replace a specific index position in a list - Python Help - Discussions on Python.org
November 3, 2021 - Hello! So, just as the title says, how do I replace a specific index position? If you look at the bottom where it says blankword.replace(blankword[i],guess), that is what Iโ€™m having trouble with. I think I know why it doโ€ฆ
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-replace-values-in-a-list-in-python
How to Replace Values in a List in Python? - GeeksforGeeks
In this method, we use lambda and map function to replace the value in the list. map() is a built-in function in python to iterate over a list without using any loop statement. A lambda is an anonymous function in python that contains a single ...
Published: January 4, 2025