Open In App

How to Replace Values in a List in Python?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to replace the value in a List using Python. We can replace values in the list in serval ways. Below are the methods to replace values in the list.

  • Using list indexing
  • Using for loop
  • Using while loop
  • Using lambda function
  • Using list slicing

Method 1: Using List Indexing

We can access items of the list using indexing. This is the simplest and easiest method to replace values in a list in python. If we want to replace the first item of the list we can di using index 0. Here below, the index is an index of the item that we want to replace and the new_value is a value that should replace the old value in the list.

Syntax:  l[index]=new_value

Code:

Python3




# Replace Values in a List using indexing
 
# define list
l = [ 'Hardik','Rohit', 'Rahul', 'Virat', 'Pant']
 
# replace first value
l[0] = 'Shardul'
 
# print list
print(l)


Output:

['Shardul', 'Rohit', 'Rahul', 'Virat', 'Pant']

Method 2: Using For Loop

We can use for loop to iterate over the list and replace values in the list. Suppose we want to replace ‘Hardik’ and ‘Pant’ from the list with ‘Shardul’ and ‘Ishan’. We first find values in the list using for loop and if condition and then replace it with the new value. 

Python3




# Replace Values in a List using For Loop
 
# define list
l = ['Hardik', 'Rohit', 'Rahul', 'Virat', 'Pant']
 
for i in range(len(l)):
 
    # replace hardik with shardul
    if l[i] == 'Hardik':
        l[i] = 'Shardul'
 
    # replace pant with ishan
    if l[i] == 'Pant':
        l[i] = 'Ishan'
 
# print list
print(l)


Output:

['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']

Method 3: Using While Loop

We can also use a while loop to replace values in the list. While loop does the same work as for loop. In the while loop first, we define a variable with value 0 and iterate over the list. If value matches to value that we want to replace then we replace it with the new value.

Python3




# Replace Values in a List using While Loop
 
# define list
l = ['Hardik', 'Rohit', 'Rahul', 'Virat', 'Pant']
 
i = 0
while i < len(l):
 
    # replace hardik with shardul
    if l[i] == 'Hardik':
        l[i] = 'Shardul'
 
    # replace pant with ishan
    if l[i] == 'Pant':
        l[i] = 'Ishan'
 
    i += 1
 
# print list
print(l)


Output:

['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']

Method 4: Using Lambda Function

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 line expression. Here we gave one expression as a condition to replace value. Here we replace ‘Pant’ with ‘Ishan’ in the lambda function. Then using the list() function we convert the map object into the list.

Syntax: l=list(map(lambda x: x.replace(‘old_value’,’new_value’),l))

Python3




# Replace Values in a List using Lambda Function
 
# define list
l = ['Hardik', 'Rohit', 'Rahul', 'Virat', 'Pant']
 
# replace Pant with Ishan
l = list(map(lambda x: x.replace('Pant', 'Ishan'), l))
 
# print list
print(l)


Output:

['Hardik', 'Rohit', 'Rahul', 'Virat', 'Ishan']

Method 5: Using List Slicing

Python allows us to do slicing inside a list. Slicing enables us to access some parts of the list. We can replace values inside the list using slicing. First, we find the index of variable that we want to replace and store it in variable ‘i’. Then, we replace that item with a new value using list slicing. Suppose we want to replace ‘Rahul’ with ‘Shikhar’ than we first find the index of ‘Rahul’ and then do list slicing and remove ‘Rahul’ and add ‘Shikhar’ in that place.

Syntax: l=l[:index]+[‘new_value’]+l[index+1:]

Python3




# Replace Values in a List using Slicing
 
# define list
l = ['Hardik', 'Rohit', 'Rahul', 'Virat', 'Pant']
 
# find the index of Rahul
i = l.index('Rahul')
 
# replace Rahul with Shikhar
l = l[:i]+['Shikhar']+l[i+1:]
 
# print list
print(l)


Output:

['Hardik', 'Rohit', 'Shikhar', 'Virat', 'Pant']

Method 6: Using functools.reduce method: 

Algorithm: 

  • Import reduce method from functools. 
  • initialize the test list. 
  • Use reduce method on the test list which checks for the element in the list and replaces it with new value. 
  • Print new list. 

Python




# Replace Values in a List using reduce
from functools import reduce
 
# define list
l = ['Hardik', 'Rohit', 'Rahul', 'Virat', 'Pant']
 
 
# replace Rahul with Shikhar
l = reduce(lambda a, b : a + ['Shikhar'] if b == 'Rahul' else a + [b], l , [])
 
# print list
print(l)


Output

['Hardik', 'Rohit', 'Shikhar', 'Virat', 'Pant']

Time complexity: O(N), where n is the length of the input List. 
Auxiliary space: O(k), where k is the length of the new list with the new value.



Last Updated : 05 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads