msh855
msh855

Reputation: 1571

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

Upvotes: 0

Views: 113

Answers (3)

Ferroao
Ferroao

Reputation: 3043

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

Upvotes: 0

sedavidw
sedavidw

Reputation: 11691

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

Upvotes: 5

Jakub Dóka
Jakub Dóka

Reputation: 2625

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

Upvotes: 0

Related Questions