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]
Written by Dmitry Belaventsev
Related protips
2 Responses
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Related Tags
#python
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#