I have a list
l = ['abc', 'abcdef', 'def', 'defdef', 'polopolo']
I'm trying to delete strings whose superstring is already in the list. In this case, the result should be:
['abcdef', 'defdef', 'polopolo']
I have written the code:
l=['abc','abcdef','def','defdef','polopolo']
res=['abc','abcdef','def','defdef','polopolo']
for each in l:
l1=[x for x in l if x!=each]
for other in l1:
if each in other:
res.remove(each)
but it doesnt seem to work. I have read that we cannot remove from the list while iterating over it. Hence the copy res, while l is my original list.
res.remove(each):) For an efficient way to do this, please check my answer :)