Try using a list comprehension and a conditional expression.

>>> a=[1,2,3,1,3,2,1,1]
>>> [4 if x==1 else x for x in a]
[4, 2, 3, 4, 3, 2, 4, 4]
Answer from outis on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_lists_change.asp
Python - Change List Items
Note: As a result of the example above, the list will now contain 4 items. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Discussions

Replace values in list using Python - Stack Overflow
Sometimes, they increase readability ... (and for Python 3 also dict) comprehensions, the ternary operator, anonymous (lambda) functions, and functions like map, zip, filter, reduce, etc. 2009-10-09T12:56:02.123Z+00:00 ... Save this answer. ... Show activity on this post. Riffing on a side question asked by the OP in a comment, i.e.: what if I had a generator that yields the values from range(11) instead of a list. Would it be possible to replace values in ... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐 r/learnpython
5
2
January 29, 2022
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
🌐 r/learnpython
6
0
September 26, 2022
Making str.replace() accept lists - Ideas - Discussions on Python.org
Syntax of the method: str.replace(old, new, count=-1) What if replace could accept two lists instead of two strings. Often I find in code: text.replace(a, b).replace(c, d) The concatenation of replace calls can cause a unnecessary performance hit if the string is large or the call is the calls ... More on discuss.python.org
🌐 discuss.python.org
3
May 9, 2020
People also ask

How do I replace one item in a Python list?
Assign to its zero-based index, such as items[index] = new_value; an invalid index raises IndexError.
🌐
pythonpool.com
pythonpool.com › home › tutorials › replace items in a python list: index, loop, map(), and slices
Replace Items in a Python List: Index, Loop, map(), and Slices
How do I replace every matching item in a list?
Use enumerate() and assign matching indexes in place, or build a new list with a conditional comprehension.
🌐
pythonpool.com
pythonpool.com › home › tutorials › replace items in a python list: index, loop, map(), and slices
Replace Items in a Python List: Index, Loop, map(), and Slices
Can I replace list items with map()?
Yes. Pass a named transformation to map() and convert the lazy iterator to list when a materialized result is needed.
🌐
pythonpool.com
pythonpool.com › home › tutorials › replace items in a python list: index, loop, map(), and slices
Replace Items in a Python List: Index, Loop, map(), and Slices
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-replace-values-in-a-list-in-python
Replace Values in a List in Python - GeeksforGeeks
This method replaces a value by directly accessing a specific position in the list and assigning a new value to that position. It works when the index of the element to be replaced is already known.
Published   January 12, 2026
🌐
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 - Replace values in a list in Python: Various methods like loops, list comprehension, and built-in functions facilitate this task effectively.
🌐
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 - Learn how to replace an item or items in a Python list, including how to replace at an index, replacing values, replacing multiple values.
🌐
Python Pool
pythonpool.com › home › tutorials › replace items in a python list: index, loop, map(), and slices
Replace Items in a Python List: Index, Loop, map(), and Slices
July 4, 2021 - Replace Python list items by index, replace every match with a loop or comprehension, use map(), and update ranges with slice assignment.
Find elsewhere
🌐
SitePoint
sitepoint.com › python hub › change list items
Python - Change List Items | SitePoint — SitePoint
The simplest way to modify a list element is to use its index and the assignment operator =. ... Here, we replaced the element at index 1 (the second element, since indexing starts at 0) from "banana" to "orange".
🌐
Python Guides
pythonguides.com › replace-items-in-a-python-list
How To Replace Values In A List Using Python?
March 19, 2025 - Learn how to replace values in a list using Python with methods like indexing, list comprehension, and `map()`. This guide includes step-by-step examples.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python replace values in list with examples
Python Replace Values in List With Examples - Spark By {Examples}
May 31, 2024 - How to replace values or elements in list in Python? You can replace a list in python by using many ways, for example, by using the list indexing, list
🌐
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 - Replace strings in Python (replace, translate, re.sub, re.subn) List comprehensions offer a simpler alternative to the traditional for loop when creating new lists.
🌐
Career Karma
careerkarma.com › blog › python › replace item in list in python: a complete guide
Replace Item in List in Python: A Complete Guide: A Complete Guide
December 1, 2023 - If you want to replace one value in a list, the indexing syntax is most appropriate. To replace multiple items in a list that meet a criterion, using a list comprehension is a good solution. While for loops are functional, they are less Pythonic than list comprehensions.
🌐
Ceos3c
ceos3c.com › home › python › python list replace – simple & easy
Python List Replace - Simple & Easy
March 1, 2023 - To do this with our favorite_colors list, we can simply use the index of “Red” (0) and replace it with "Black". favorite_colors[0] = "Black" print(favorite_colors)Code language: Python (python)
🌐
Reddit
reddit.com › r/learnpython › how to replace an item in a list without indexing
r/learnpython on Reddit: How to replace an item in a list without indexing
January 29, 2022 -

I have this list:

a =[1,2,3,4,5,6,7,8,9]

I want to replace a specific number in the list with the string 'X' without indexing. For example I ask for user input for which number he wants to replace

userinput=input('please choose a number from 1-9')
a.replace(int(userinput),"X")

python says "'list' object has no attribute 'replace'" so replace can't be used on a list, but I can't think of any other way to do this

🌐
Replit
replit.com › discover › how-to-replace-a-element-in-list-in-python
Discover | Replit
Build and deploy software collaboratively with the power of AI without spending a second on setup.
🌐
Appdividend
appdividend.com › python-list-replace
How to Replace Elements in a Python List
August 19, 2025 - Once we have an index, we can use the list indexing to replace the element, which in our case is 21. The updated list has “21” element. The most Pythonic way to replace multiple values in a list is to use a list comprehension.
🌐
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 › ideas
Making str.replace() accept lists - Ideas - Discussions on Python.org
May 9, 2020 - Syntax of the method: str.replace(old, new, count=-1) What if replace could accept two lists instead of two strings. Often I find in code: text.replace(a, b).replace(c, d) The concatenation of replace calls can cause…
🌐
stataiml
stataiml.com › posts › 35_find_replace_string_python_list
Find and Replace Values in List in Python - stataiml
May 3, 2024 - Learn how to find and replace string values in list in Python using list comprehension and `map()` functions
🌐
Real Python
realpython.com › lessons › replacing-elements-list
Replacing Elements in a List (Video) – Real Python
00:00 One of the reasons lists are so versatile is that Python provides many ways to mutate or change them. In this lesson, you’ll look at using assignment to update lists. Like you saw in the previous lesson, you can replace a single element using index assignment.
Published   June 17, 2025