I'm trying to remove all instances of a certain string from a list which contains sublists. For example, something like this:
myarray = ['a', 'a', ['b', 'a', 'a'], ['a', 'c', 'd', 'a'], 'a', ['a', 'd']]
ends up like this
mylist = [['b'],['c','d'],['d']]
after removing all the instances of 'a'
.
I have used this code:
def delnodata(lst, what):
for index, item in enumerate(lst):
if type(item) == list:
delnodata(item, what)
else:
if item == what:
lst.remove(item)
delnodata(mylist, 'a')
but the output is:
[['b', 'a'], ['c', 'd'], 'a', ['a', 'd']]
I've seen a lot of similar questions on this site, but unfortunately my programming skills aren't good enough to put this together myself!