I am searching if values from list 1 are available in list2 or not. if whatever values from list 1 are found in list2 then I want to replace all those values in list2 by a single value.
list1 = ['banana', 'apple', 'cat', 'peacock']
list2 = ['hello', 'apple', 'cat', 'sherrif']
Solution I tried:
for i,item in enumerate(list2):
if item in list1:
list2[i]= 'cat'
print(list2)
Current output:
replaced every value in list2 by jackie
list2 = ['hello', 'jackie', 'jackie', 'sherrif']
Expected: Output: As values like apple, cat from list1 are available in list2 so in output, both should be replaced by single value jackie. if no value found list1 found in list2 then list2 remains same
list2 = ['hello', 'jackie', 'sherrif']
list2=['banana', 'apple', 'peacock', 'cat']? Do you replace it with['banana', 'jackie', 'peacock', 'jackie']or['banana', 'jackie', 'peacock']? In other words, do you replace only the first value that is also contained inlist1and remove all the reset, or do you replace each consecutive sequence of values also contained inlist1with a separate"jackie"?item in list1is O(len(list1)) since it searches all elements oflist1. The total algorithm becomes O(len(list1)*len(list2)), which is OK if either one of the lists are small, but becomes a disaster if both lists are big. Solution: createset1=set(list1)and work with that.