I have a list with many words (100.000+), and what I\'d like to do is remove all the substrings of every word in the list.
So for simplicity, let\'s imagine that I have
You can sort your data by length, and then use a list comprehension:
words = ['Hello', 'Hell', 'Apple', 'Banana', 'Ban', 'Peter', 'P', 'e']
new_words = sorted(words, key=len, reverse=True)
final_results = [a for i, a in enumerate(new_words) if not any(a in c for c in new_words[:i])]
Output:
['Banana', 'Hello', 'Apple', 'Peter']