Last Updated: February 25, 2016
·
1.305K
· dizpers

Get generality of two lists in python

>>> list1 = ['a','b','c']
>>> list2 = ['q','a','b']
>>> [x for x in list1 if any(x == y for y in list2)]
['a', 'b']

as @frol commented, there is a better solution:

[x for x in list1 if x in list2]

2 Responses
Add your response

WTF?! Use operator in instead...

>>> [x for x in list1 if x in list2]
over 1 year ago ·

@frol Thanks! It was really simplier. :)

over 1 year ago ·