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:
listA = ['a', 'b', 'c']
listB = ['a', 'h', 'c']
I’d like to be able to return:
['a', 'c']
How so?
Possibly with variable strings like:
john = 'I love yellow and green'
mary = 'I love yellow and red'
And return:
'I love yellow and'
Use set intersection for this:
gives:
Note that since we are dealing with sets this may not preserve order:
using
join()to convert the resulting list into a string.—
For your example/comment below, this will preserve order (inspired by comment from @DSM)
For a case where the list aren’t the same length, with the result as specified in the comment below: