0

How to look up if a string in one list is a part of another list:

b_names = ['robert', 'jon', 'arya']
a_names = ['rya', 'fish']


def filterA(name):
for string in b_names:
    if name in string:
        return True
    else :
        return False

final_list = filter(filterA,a_names)

The final_list is empty and should have contained the string rya since rya is present as a substring in the arya from the first list.

What is the error here ?

1 Answer 1

1

Your for loop is ending too early.

If name is not in string, it returns False. With Robert being first item in b_names, it ends the loop and does not continue to Jon or Arya. You need to put your return False after your for loop

b_names = ['robert', 'jon', 'arya']
a_names = ['rya', 'fish']


def filterA(name):
    for string in b_names:
        if name in string:
            return True

    return False


final_list = filter(filterA, a_names)
print final_list

>>> ['rya']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for spotting I think I somehow have overlooked the filterA logic. Thanks much

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.