So to give a rough example without any code written for it yet, I\'m curious on how I would be able to figure out what both lists have in common.
Example:
Use set intersection for this:
list(set(listA) & set(listB))
gives:
['a', 'c']
Note that since we are dealing with sets this may not preserve order:
' '.join(list(set(john.split()) & set(mary.split())))
'I and love yellow'
using join()
to convert the resulting list into a string.
--
For your example/comment below, this will preserve order (inspired by comment from @DSM)
' '.join([j for j, m in zip(john.split(), mary.split()) if j==m])
'I love yellow and'
For a case where the list aren't the same length, with the result as specified in the comment below:
aa = ['a', 'b', 'c']
bb = ['c', 'b', 'd', 'a']
[a for a, b in zip(aa, bb) if a==b]
['b']