Hàm replace[] trong Python trả về một bản sao của chuỗi ban đầu sau khi đã thay thế các chuỗi con cũ bằng chuỗi con mới.replace[] trong Python trả về một bản sao của chuỗi ban đầu sau khi đã thay thế các chuỗi con cũ bằng chuỗi con mới. Cú pháp của replace[] trong Python:replace[] trong Python: Các tham số: old: Đây là chuỗi con cũ để được thay thế.: Đây là chuỗi con cũ để được thay thế. new: Đây là chuỗi con mới để thay thế cho chuỗi con cũ.: Đây là chuỗi con mới để thay thế cho chuỗi con cũ. max: Nếu tham số tùy ý max này được cung cấp, thì chỉ có các sự xuất hiện đầu tiên được thay thế.: Nếu tham số tùy ý max này được cung cấp, thì chỉ có các sự xuất hiện đầu
tiên được thay thế. Ví dụ sau minh họa cách sử dụng của hàm replace[] trong Python. Chạy chương trình Python trên sẽ cho kết quả: Cú pháp
str.replace[old, new[, max]]
str1 = "Vi du ham replace[] Python"
print [str1.replace["Python", "Python tren VietTuts.Vn"]]
print [str1.replace["ham", "phuong thuc", 1]]
Vi du ham replace[] Python tren VietTuts.Vn
Vi du phuong thuc replace[] Python
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 l[index]=new_value
Code:
Python3
l
=
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]0
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]1
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]2
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]3
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]5
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]7
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]9
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python0
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python1
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python2
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python3
=
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python5
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python6
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python7
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
l
=
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Pant']0
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]1
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]3
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]5
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]7
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]9
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python0
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']1
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']2
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']3
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']4
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']5
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']6
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']7
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']8
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']9
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']0
=
=
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]1
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']4
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']5
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']0
=
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python5
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']8
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']9
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']0
=
=
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]9
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']4
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']5
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']0
=
['Hardik', 'Rohit', 'Rahul', 'Virat', 'Ishan']9
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python6
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python7
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
l
=
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Pant']0
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]1
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]3
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]5
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]7
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]9
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python0
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']1
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']2
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']3
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']4
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']5
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']6
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']7
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']8
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']9
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']0
=
=
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]1
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']4
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']8
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']9
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']0
=
=
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]1
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']4
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']5
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']0
=
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python5
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']8
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']9
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']0
=
=
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]9
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']4
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']5
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']0
=
['Hardik', 'Rohit', 'Rahul', 'Virat', 'Ishan']9
Method 3: Using While Loop
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python6
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python7
Output:
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']
2=
Vi du ham replace[] Python tren VietTuts.Vn
Vi du phuong thuc replace[] Python
2
l
8 l
9
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']6
=
1
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']8['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']2str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]16=
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]18l=list[map[lambda x: x.replace[‘old_value’,’new_value’],l]]
Python3
l
=
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Pant']0
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]1
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]3
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]5
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]7
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]9
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python0
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']1
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']2
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']3
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']4
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']5
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']6
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']7
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python6
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python7
Output:
['Hardik', 'Rohit', 'Rahul', 'Virat', 'Ishan']
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']
8['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']
9 ['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']
0=
=
str1 = "Vi du ham replace[] Python"
print [str1.replace["Python", "Python tren VietTuts.Vn"]]
print [str1.replace["ham", "phuong thuc", 1]]
1['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']
4
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']5
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']0
=
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python5
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']8['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']9['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']0=
=
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]9['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']4l=l[:index]+[‘new_value’]+l[index+1:]
Python3
l
=
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Pant']0
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]1
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]3
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]5
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]7
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]4
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]9
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python0
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Ishan']2
=
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]63
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]5
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]65
l
=
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]68
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]16
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Pant']0 ____171
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python0
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]16
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]74
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]16
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]18
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]77
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python6
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python7
Output:
['Hardik', 'Rohit', 'Shikhar', 'Virat', 'Pant']