'Checking if a list element is inside another list if so, substitute every element inside the base list
So I currently have a bunch of mocks list such as this one. Sample:
l = ['a','a','c','d']
l2 = ['c','b','b','d']
l3 = ['c','b','b','d','x','z']
l4 = ['a','c','a','a']
What I want to do is check if any of the values inside each of these lists, is equal to any of the values inside another list.
target_l = ['a','b']
If so i want to change every element inside these lists into the matched element.
Wanted result:
l = ['a','a','a','a']
l2 = ['b','b','b','b']
l3 = ['b','b','b','b','b','b']
l4 = ['a','a','a','a']
Solution 1:[1]
Here is how you can do it for l:
for e in target_l:
if e in l:
l = [e]*len(l)
break
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Maxime |
