There’s nothing built-in, but it’s just a loop to do the replacement in-place:
for i, word in enumerate(words):
if word == 'chicken':
words[i] = 'broccoli'
or a shorter option if there’s always exactly one instance:
words[words.index('chicken')] = 'broccoli'
or a list comprehension to create a new list:
new_words = ['broccoli' if word == 'chicken' else word for word in words]
any of which can be wrapped up in a function:
def replaced(sequence, old, new):
return (new if x == old else x for x in sequence)
new_words = list(replaced(words, 'chicken', 'broccoli'))
Top answer 1 of 2
31
There’s nothing built-in, but it’s just a loop to do the replacement in-place:
for i, word in enumerate(words):
if word == 'chicken':
words[i] = 'broccoli'
or a shorter option if there’s always exactly one instance:
words[words.index('chicken')] = 'broccoli'
or a list comprehension to create a new list:
new_words = ['broccoli' if word == 'chicken' else word for word in words]
any of which can be wrapped up in a function:
def replaced(sequence, old, new):
return (new if x == old else x for x in sequence)
new_words = list(replaced(words, 'chicken', 'broccoli'))
2 of 2
5
No such method exists, but a list comprehension can be adapted to the purpose easily, no new methods on list needed:
words = 'I like chicken'.split()
replaced = ['turkey' if wd == "chicken" else wd for wd in words]
print(replaced)
Which outputs: ['I', 'like', 'turkey']
GeeksforGeeks
geeksforgeeks.org › python › how-to-replace-values-in-a-list-in-python
Replace Values in a List in Python - GeeksforGeeks
Given a list of elements and a value to replace, the task is to update one or more matching values with a new value. For Example: Input: a = [10, 20, 30, 40, 50] and Replace value 30 -> 99 Output: [10, 20, 99, 40, 50]
Published January 12, 2026
How to replace values in a list?
number is different from board. Print the id for each. for row in board: for number in row: if board[row][number]== 0: board[row][number]=" " More on reddit.com
Hi! I am a newbie. Why doesn't list.replace() work? Help!
Hey! I was working on a text adventure game and was trying to remove a part of dis3 after the player takes the item in isect3(). Here is my code Help = "NO CAPS l=look n=north s=south e=east w=west u=up d=down ew=eastwest take (item) l (item) use (item) r=restart drop (item) take all" inventory ... More on discuss.python.org
How to replace an item in a list without indexing
a = [num if num != int(userinput) else "X" for num in a] More on reddit.com
MATLAB function "publish": How to replace it in Python?
I have decided to overwrite my comments. More on reddit.com
Videos
15:33
List Replace Python - The EASIEST Methods - YouTube
03:59
Replace A Range Of List Items | Python Tutorial - YouTube
01:49
How to Replace Values in a List | Python Tutorial - GeeksforGeeks ...
13:47
Replacing a String In a String in Python - YouTube
20:26
How to Replace Values in a List in Python? - GeeksforGeeks | Videos
02:20
Python - How to replace values in a list of a list using a dictionary ...
Analytics Vidhya
analyticsvidhya.com › home › how to replace values in a list in python?
How to Replace Values in a List in Python? - Analytics Vidhya
April 23, 2025 - When replacing values in a list in Python, it’s important to consider the data types involved. If the new value has a different data type than the original, you may need to perform type conversion. For example, if you want to replace an integer with a string, you can use the `str()` function to convert the integer to a string.
W3Schools
w3schools.com › python › ref_string_replace.asp
Python String replace() Method
Remove List Duplicates Reverse ... Q&A Python Bootcamp Python Training ... The replace() method replaces a specified phrase with another specified phrase....
Reddit
reddit.com › r/learnpython › how to replace values in a list?
How to replace values in a list? : r/learnpython
September 26, 2022 - As long as you're not inserting or deleting items from a list, you can use the enumerate function to generate an index for each element in the list. That index can be used to modify an element. board = [] for i in range(3): row = [] for j in range(3): row.append(0) board.append(row) print(board) for row in board: for i, number in enumerate(row): if number == 0: row[i] = " " print(board) ... Starting Python today.
Python.org
discuss.python.org › python help
Hi! I am a newbie. Why doesn't list.replace() work? Help! - Python Help - Discussions on Python.org
March 19, 2022 - Hey! I was working on a text adventure game and was trying to remove a part of dis3 after the player takes the item in isect3(). Here is my code Help = "NO CAPS l=look n=north s=south e=east w=west u=up d=down ew=eastwest take (item) l (item) use (item) r=restart drop (item) take all" inventory = ['$10 bill'] appearance = "dirty" #1 sect1 = ["l", "n", "s", "l door"] sect2 = ["l", "w", "s", "l shelf"] sect3 = ["l", "n", "s", "e", "w", "l shovel", "l flo...
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 - # Replace a particular item in a Python list using a function a_list = ['aple', 'orange', 'aple', 'banana', 'grape', 'aple'] def replace_values(list_to_replace, item_to_replace, item_to_replace_with): return [item_to_replace_with if item == item_to_replace else item for item in list_to_replace] replaced_list = replace_values(a_list, 'aple', 'apple') print(replaced_list) # Returns: ['apple', 'orange', 'apple', 'banana', 'grape', 'apple']
Ceos3c
ceos3c.com › home › python › python list replace – simple & easy
Python List Replace - Simple & Easy
March 1, 2023 - for index, color in enumerate(favorite_colors): if color == "Gray": favorite_colors[index] = "Beige" print(favorite_colors)Code language: Python (python) ... This way, we utilize a for loop in combination with the enumerate() function to locate the color "Gray" and replace it with the color "Beige". Breaking this down, the enumerate() function requires three things: ... The loop then runs over each item of our list and checks if the color is, in fact, "Gray".
GeeksforGeeks
geeksforgeeks.org › how-to-replace-values-in-a-list-in-python
How to Replace Values in a List in Python? - GeeksforGeeks
The simplest way to replace values in a list in Python is by using list indexing. Let’s take an example of replacing values in a list using list indexing. ... Let’s see other different methods to Replace Values in a List. ... In this method, we use lambda and map function to replace the value in the list.
Published January 4, 2025
Interview Kickstart
interviewkickstart.com › home › blogs › learn › the replace() function in python
The replace() Function in Python | Interview Kickstart
September 25, 2024 - We can replace a string in a list in Python by using the replace() function along with a for loop.
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.
Python Help
pythonhelp.org › python-lists › how-to-replace-in-a-list-python
How to replace items in a list in Python
# List of items list_items = ['apple', 'banana', 'cherry', 1, 2, 3] # Elements to replace (can be any data type) elements_to_replace = ['apple', 1] # New elements new_elements = ['mango', 4] # Replacing in a list list_items = list(map(lambda x: new_elements[elements_to_replace.index(x)] if x in elements_to_replace else x, list_items)) print(list_items) # ['mango', 'banana', 'cherry', 4, 2, 3] In both examples above, we replace the elements 'apple' and 1 in our original list with 'mango' and 4 respectively.
GeeksforGeeks
geeksforgeeks.org › python › python-replace-substring-in-list-of-strings
Replace substring in list of strings - Python - GeeksforGeeks
July 11, 2025 - For example, given a = ["hello ... discuss different methods to do this in Python. replace() method replaces occurrences of a substring in a string....
Python Guides
pythonguides.com › replace-items-in-a-python-list
How To Replace Values In A List Using Python?
March 19, 2025 - Here, we use map() with a lambda function that checks if each state starts with the letter “M” using the startswith() method. If true, the state is replaced with “Massachusetts”. Otherwise, the original state name is retained. Finally, we convert the result of map() back to a list using the list() function. Check out How to Remove None Values from a List in Python?