-1

I need to find the common element from 2 lists, and if there is none, print []. I managed to do the first part, but when I add else it goes sideways...

def find_common(L1,L2):
   for element in L1:
        if element in L2:
            return (element)
        else :   
            return []



L1 = ['Dancing', 'Computers', 'Rowing']
L2 = ['Computers', 'Books', 'Movies']

print(find_common(L1,L2)) #should print "Computers"

L1 = ['Fishing'] 
L2 = ['Swimming']

print(find_common(L1,L2)) #should print []

The output is for this code is [][] right now. If I don't add the else function, the output is "Computer", which is correct. The problem is that I need to check also some lists that don't have a common element and output [], and here it goes wrong. What is my mistake? Thank you.

3 Answers3

1

Note that return will exit the function, so you only want to have one in the if statement. Also checking membership with sets will work faster:

def find_common(L1,L2):
    s2 = set(L2)
    for element in L1:
        if element in s2:
            return element
    else:
        return []

find_common(L1, L2)
# ['Computers']
yatu
  • 86,083
  • 12
  • 84
  • 139
  • The set solution is not necessarily cheaper since we only have to find one intersection element and can then return. – Philipp Oct 16 '19 at 12:19
  • In the case that there's only one common element, yes it could be @Philipp – yatu Oct 16 '19 at 12:20
  • The question says "I need to find the common element" this is also why he immediately returns in his loop when one element is found. – Philipp Oct 16 '19 at 12:21
  • Ah yes you're right in that case, then the only unnecessary part is the `else` statement @philip – yatu Oct 16 '19 at 12:23
  • One can have several returns in a function. Note that if there is no common element found in your loop there will be an implicit return None. Also using set is only worthwhile if there are duplicates I think. – Philipp Oct 16 '19 at 12:28
  • That only answers the first part, if there are any matches, but at the second part, if there are not any, it should return [], that is with what I was struggling. – Adrian Stefan Oct 16 '19 at 12:30
  • For also has an `else` statment, check update @AdrianStefan – yatu Oct 16 '19 at 12:46
0

Hi It's normal that you get [ ], because return will break (exit) your code

 def find_common(L1,L2):
     for element in L1:
        if element in L2:
           print(element)

And if you want to get a list of similar word from your code you have to initialize a empty list that you fill in your loops

 def find_common(L1,L2):
     Similar = []
     for element in L1:
        if element in L2:
           Similar.append(element)
     return Similar
Jad
  • 109
  • 10
0

Your function find_common returns [] already if only one of the elements in L1 is not present in L2. Therefore you should only return [] if none of the elements in L1 are in L2, that is when you ran through the whole loop:

def find_common(L1,L2):
   for element in L1:
        if element in L2:
            return (element)
   return []
Philipp
  • 1,191
  • 1
  • 14
  • 16
  • that did the trick, maybe it is a stupid question, but can you explain what is the difference between my code and your code? only the return part, or in my case, else, thank you. – Adrian Stefan Oct 16 '19 at 12:27
  • The difference is that my return is **after** the loop, while your return is **in** the loop. The moment that one element of L1 is not found in L2 your function already returns **prematurely**. – Philipp Oct 16 '19 at 12:34