def ReplaceAllInList(list, lookupvalue, replacewith):
return [(replacewith if v == lookupvalue else v) for v in list]
a = ['aaa', 'bb', 'bb']
a = ReplaceAllInList(list= a, lookupvalue= 'bb', replacewith= 'cc')
print(a) # output --> ['aaa', 'cc', 'cc']
# Replace 'Koweit' To 'Sudan'
my_list = [ "Egypt", "Koweit", "Algeria", "Morocco", "Tunisia" ]
my_list[ 1 ] = "Sudan"
print( my_list )
# Replace a particular item in a Python list
a_list = ['aple', 'orange', 'aple', 'banana', 'grape', 'aple']
for i in range(len(a_list)):
if a_list[i] == 'aple':
a_list[i] = 'apple'
print(a_list)
# Returns: ['apple', 'orange', 'apple', 'banana', 'grape', 'apple']
# Replace Values in a List using Slicing
# define list
l = ['Hardik', 'Rohit', 'Rahul', 'Virat', 'Pant']
# find the index of Rahul
i = l.index('Rahul')
# replace Rahul with Shikhar
l = l[:i]+['Shikhar']+l[i+1:]
# print list
print(l)