I'm trying to create a recursive function which takes an input list (with nested lists) and removes all elements that match elements from a second input array. I currently have this:
def removeElements(self, array, remove_elements):
for element in array:
if isinstance(element, list):
removeElements(element, remove_elements)
else:
for remove_element in remove_elements:
if element == remove_element:
array.remove(element)
return array
The 'array' input generally looks something like this:
array = [[1], [[2,3,4],[5,6,7]], [8,9,10]]
And the 'remove_elements' array is never nested and looks something like this:
remove_elements = [2,4,6,8,10]
This generally works as expected until I give the following remove_elements
array:
[1,2,3,4,5,6,7,8,9,10]
Output:
[[], [[3], [6]], [9]]
For some reason, when I try to run it on every single element it doesn't remove the 2nd index of nested lists that have more than 1 element. I would like the function to also just return a completely empty list if that's possible, but I don't know what I'm doing wrong.