python list item replace 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 an element of a list using list comprehension
new_list = ["A" if element == "a" else element for element in a_list]