how to replace a value in a list code example
Example 1: how to replace string in list python
Use str. replace() to replace a string in a list
strings = ["a", "ab", "aa", "c"]
new_strings = []
for string in strings:
new_string = string. replace("a", "1") Modify old string.
new_strings. append(new_string) Add new string to list.
print(new_strings)
Example 2: how to replace number in list python
>>> numbers = [1 , 2, 3, 4, 4, 6] # a normal python list
>>> numbers[4] = 5 # replace the redundant item (5th item)
>>> numbers
[1, 2, 3, 4, 5, 6]