septimus
septimus

Reputation: 13

Comparing lists with lists of lists

How can I check if a string of list2 is insinde a string of a list of list1?

For example:

list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]

Upvotes: 1

Views: 62

Answers (3)

Rudrransh Saxena
Rudrransh Saxena

Reputation: 36

A simple solution using loops

list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]
for x in range(len(list1)):
    for y in range(len(list2)):
        if str(list1[x]).find(str(list2[y])) == -1 :
            print("NO")
        else :
            print("YES")

Upvotes: 1

sehan2
sehan2

Reputation: 1835

here you go:

list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]
res = [[l in e[0] for l in list2] for e in list1]

Upvotes: 0

swor
swor

Reputation: 901

Function returns true if element of list2 exists in list1

    def my_find(list1, list2):
        for cur in list2:
            for cur_list in list1:
                if cur in cur_list:
                    return True
        return False

Upvotes: 1

Related Questions