0

I want to filter from a list all tuples that don't have specific elements. Specifically, suppose that:

mylist = [("sagap", "apple", "orange"), ("apple", "orange", "crazy"), ("crazy", "orange"), ("orange", "banana", "do", "does"), ("apple", "do", "does", "something", "response")]

I want to exclude/remove all those tuples in the list that don't contain "apple"' and "orange"' within a tuple

The expected result should be a new list with tuples as below:

mylist_new = [("sagap", "apple", "orange"), ("apple", "orange", "crazy") ] 

I would appreciate your help. Please consider in my actual project the list has around 10000 tuples.

Ideally, I want to have something like:

list_of_items = ["apple, "orange"] 

search in my list which tuples have list_of_times and keep those in my list 

Please consider the number of items might not necessarily be just two, could be any large number of items to consider

1
  • Have you tried with any code so far?
    – Salty 27
    Apr 26, 2021 at 18:10

3 Answers 3

5

You can do

mylist_new = [t for t in mylist if 'apple' in t and 'orange' in t]

Your example has some typos... changing it to

In [9]: mylist = [("sagap", "apple", "orange"), ("apple", "orange", "crazy"), ("crazy", "orange"), ("orange", "banana", "do", "does"), ("apple", "do", "does", "something", "response")]

In [10]: mylist_new = [t for t in mylist if 'apple' in t and 'orange' in t]

In [11]: mylist_new
Out[11]: [('sagap', 'apple', 'orange'), ('apple', 'orange', 'crazy')]

UPDATE OP updated question to allow ask for an arbitrary list of items

To accommodate this I would simply write a function that checks those in a loop

def contains_items(t, list_of_items):
    for i in list_of_items:
        if i not in t:
           return False
    return True

Then my original answer would be

list_of_items = ["apple", "orange"] 
mylist_new = [t for t in mylist if contains_items(t, list_of_items)]
3
  • Thank you. I would appreciate if you can help with the update. The reason that I don't like this solution very much is that the number of items I might need to look at might be more than 2 like 5 or 10 for example. I will update my question.
    – msh855
    Apr 26, 2021 at 18:15
  • 1
    The solution doesn't change much you just need to write a function that still determines if a tuple meets your conditions, I've updated the response
    – sedavidw
    Apr 26, 2021 at 18:22
  • For the modified 'arbitrary list of items' version, you can use all(item in t for item in list_of_items) - either as the if clause or as the body of contains_items.
    – aneroid
    Apr 26, 2021 at 18:34
0

you can also use filter function:

myList = [( "apple", "orange"), ("apple", "orange", "crazy"), ("crazy", "orange"), ("orange", "banana", "do", "does"), ("apple", "do", "does", "something", "response")]

def valid(t):
    return "apple" in t and "orange" in t

print(list(filter(valid, myList)))

its bit more readable

0

Another oneliner:

myList = [( "apple", "orange"), ("apple", "orange", "crazy"), ("crazy", "orange"), ("orange", "banana", "do", "does"), ("apple", "do", "does", "something", "response")]

print(list(filter(lambda t: "apple" in t and "orange" in t, myList)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.