0

have a question.

list1 = ['the quick brown fox', 'brown fox', 'fox']
list2 = ['the', 'james went to the library', 'james']

For list1, would like to remove 'brown fox' and 'fox' since it exists in list1[0]. For list2, would like to remove 'james' and 'the' from the list since it exists in list2[1]

How do I do a check for this? The order for the terms in the list may be given in any order. Have a large number of lists as such so would appreciate the answers.

Have tried to do a for loop, going through the list but got stuck on the logic part.

for x in range(len(list1)):
    if list1[x] in list1: ## got stuck here on the logic.
datajem
  • 3
  • 2

1 Answers1

0

If I understood your question properly, you can sort the list according to the length of the strings in it ascending to have the shortest string at the beginning and check if it's exist in the longer strings after it. If so, append it into a list to take the difference between it and the original list.

You can do something like this:

list1 = ['the quick brown fox', 'brown fox', 'fox']
list2 = ['the', 'james went to the library', 'james']
lists = [list1, list2]
sol = []
for lst in lists:
  diff = []
  lst = sorted(lst, key=len)
  for i in range(len(lst)):
    for j in range(i, len(lst)):
      if i != j and lst[i] in lst[j]:
        diff.append(lst[i])
  sol.append(list(set(tuple(lst)) - set(tuple(diff))))

for lst in sol:
  print(lst)

Note: I put the lists in one list to make the solution more general.

It's not the best solution, but I think it's easy to understand.

Hope this will help.

Kasper
  • 588
  • 3
  • 16
  • Thanks Kasper. It worked well. Kinda was thinking if there was a simpler solution, but nonetheless I have implemented it in my program. Cheers! – datajem May 15 '20 at 12:53
  • You're so welcome! If this helped you, please mark it off as an answer (the checkmark). – Kasper May 15 '20 at 21:39