0
def func(l1, l2):
result = False
for x in l1:
    for y in l2:
        if x == y:
            result = True
            return result
        

print(func([1,2,3,4,5], [5,6,7,8,9])) >> True

print(func([1,2,3,4,5], [6,7,8,9])) >> None

Why does the function return None in the second case? It should return 'result' which is True or False.

1
  • 3
    You need to return result after your loop. Commented Jun 27, 2020 at 5:30

4 Answers 4

2

The control statement doesn't reach

    result = True
    return result

So the function has nothing to return, so it returns None.

This will return either True or False

def func(l1, l2):
    result = False
    for x in l1:
        for y in l2:
            if x == y:
                result = True
    return result
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please explain more clearly why this is the case? I see that the function returns result which in the case there is no equal items should stay False and returned as such.
Look at it this way: What happens after the loop has ended? Is anything returned after the loop is complete if none of the elements are found equal? Simply No. The function returns empty. If you had said return result even after the loop has ended, then it would return false.
2

May I suggest a much cleaner (and faster) solution? One of Python's strengths is list comprehension.

def func(l1, l2):
    return any([x==y for x in l1 for y in l2])

print(func([1,2,3,4,5], [5,6,7,8,9]))
print(func([1,2,3,4,5], [6,7,8,9])) 

Output:

True
False

1 Comment

Completely agree that your solution is much more "pythonic" but forgetting to place code outside of nested loops is common and instructive for someone getting started.
1

As your function is currently written, it only works when there is a pairwise match between at least one of the elements of l1 and l2 - it would correctly set result from False to True, then return the result. However, if none of the elements in l1 and l2 are pairwise equal, the return statement is never reached because your function will exit the loop and then terminate the function - without returning anything.

Therefore, the return statement needs to be outside of the loop. Otherwise, your function won't return anything if none of the elements in the lists are equal.

def func(l1, l2):
    result = False
    for x in l1:
        for y in l2:
            if x == y:
                result = True
    return result

3 Comments

but the function returns result which is False in the case there is no equal elements
Isn't that what you wanted? I ran your test cases and got: print(func([1,2,3,4,5], [5,6,7,8,9])) >> True; print(func([1,2,3,4,5], [6,7,8,9])) >> False
actually I wnated to understand why this is happening. I have at least 2 other workable solutions but it is not what I need. I need to undersatnd the code
0

So let's walk through what happens here. You check each element of l1 and l2 and if any of them are equal you return something. For now, just leave it as something. Which means in this piece of code-

if x == y:
    result = True
    return result

You only enter do result = True and return result statement if x == y. If there is no scenario where x == y occurs as it happens in the second example you mentioned. There is a variable called result = False but there is no return statement triggered. Which means it does not return anything. So, you get a None.

Right way to do this

You can make this edit in your code to make it work like you asked for-

def func(l1, l2):
    result = False
    for x in l1:
        for y in l2:
            if x == y:
                result = True
                return result
    return result

What this does is instead of returning the None if the inner return statement is not triggered it returns result which would be False.

Comments

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.