-2

Write a function printCommonLetters()that has two input arguments:
lst1 and lst2, which are two lists of strings.
The function will print the items that are the same in both lists.
When you are done iterating over both loops, print a final statement like ‘done’ or ‘goodbye’.

We discussed in class how execution resumes with non-indented statement that is aligned with for in the for loop statement.

For example if

lst1 = [ ‘ab’, ‘cd’, ‘ef’, ‘gh’] 
lst2 = [‘abc’, ’geh’, ‘cd’, ‘ab’], 

the function will print:

‘ab’

‘cd’  
‘goodbye!’ 

I have found shorthanded way to do it, but not a full function. I have it giving me back ['ab', 'abc'], but cant get it to give me the 'cd'...

here is what I have so far:

def printCommonLetters( lst1, lst2):
    for i in lst1:
       for j in lst2:
            if i is not j:
               return[i,j]

print( printCommonLetters( [ 'ab', 'cd', 'ef', 'gh'],['abc', 'geh', 'cd', 'ab'] ))

print('goodbye!')
Deduplicator
  • 44,692
  • 7
  • 66
  • 118

1 Answers1

0

That's what you need. No iteration, just set.

print set(list1)&set(list2)
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • That gave me the output I wanted! thank you so much!! is there a way to make it go down instead of... {'ab', 'cd'} but would give: 'ab' 'cd' not sure why she wants it that specific format, but I appreciate the help to get the outcome! – VincentC Oct 13 '15 at 21:18