Search code examples
pythonlisttuples

Remove tuples from a list according to criteria


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


Solution

  • 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)]