0

The answer to my question is probably somewhere around here but I couldn't find it.

I have a two lists :

['batman','superman','spiderman',]
['batman','ironman','superman','flash','wonderwoman']

I want to compare the two lists and return the matching elements as a third list :

['batman','superman']

I only found this solution:

list=['a cat','a dog','a yacht']
string='a cat'
if string in list:
  print 'found a cat!'

But it's only a comparison between a string and a list...

Simon Breton
  • 2,638
  • 7
  • 50
  • 105

1 Answers1

2

Use intersection,

l1 = ['batman','superman','spiderman',]
l2 = ['batman','ironman','superman','flash','wonderwoman']

print(set(l1).intersection(set(l2)))
#set(['batman', 'superman'])
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134