Lists are very flexible as a data structure. Unlike strings that are immutable, lists are mutable in Python. That is, you can make changes to a list after creating it. In this tutorial, we will look at how to replace an item in a Python list with the help of some examples.
How to replace item in a list?

There are a number of ways you can replace an item in a list –
- If you know the index of the item to be replaced, you can directly assign the new value to the list value at that particular index.
- If you don’t know the index of the item to be replaced,
- You can loop through the list to find the item to be replaced and replace it with the new value.
- Or, you can use a list comprehension with a conditional to check for the value to be replaced and replace it accordingly.
Examples
Let’s look at some examples of usage of the above-mentioned methods to replace items in a list in Python.
Using the item index
If you know the index of the item to be replaced, the task becomes a straightforward assignment operation. Assign the new element to the index of the item to be replaced in the list.
# create a list ls = [1, 2, 3, 4] # replace 3 with 7 using the index of 3 ls[2] = 7 # display the list print(ls)
Output:
[1, 2, 7, 4]
Here we knew the index of the item to be replaced (we wanted to replace 3 which was at index 2). The list now has 7 in place of 3.
Using a loop to find and replace value in the list
If you don’t know the index of the value to be replaced, you can iterate over the values of the list and find the element to be replaced and if found, assign the new value to its index.
# create a list ls = [1, 2, 3, 4] # replace 3 with 7 for i in range(len(ls)): # check if the item is the item to be replaced if ls[i] == 3: # replace the value at its index ls[i] = 7 # display the list print(ls)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
[1, 2, 7, 4]
We get the same result as above, 7 in place of 3 in the list.
Using list comprehension
You can use list comprehension to create a new list with the items replaced. Use a boolean condition to check for the presence of the matching item. Here’s an example.
# create a list ls = [1, 2, 3, 4] # replace 3 with 7 ls = [7 if item == 3 else item for item in ls] # display the list print(ls)
Output:
[1, 2, 7, 4]
We get the same result as above, 3 has been replaced by 7 in the resulting list. Note that here we are not exactly replacing the value in the original list rather we are creating a new list with the values replaced and assigning it to the original list.
You might also be interested in –
Python – Check if an element is in a list
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.