oomxcq
Last Updated: February 25, 2016
·
588
· dizpers
3845a56

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]
Say Thanks
Respond

2 Responses
Add your response

3624

WTF?! Use operator in instead...

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

@frol Thanks! It was really simplier. :)

over 1 year ago ·