replace an item 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)
## ['changed', 'unchanged']