replace an element in a list python 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: change element in list python
list = ['unchanged', 'unchanged']
list[0] = 'changed'
print(list)
Example 3: how to replace number in list python
>>> numbers = [1 , 2, 3, 4, 4, 6]
>>> numbers[4] = 5
>>> numbers
[1, 2, 3, 4, 5, 6]