Finding if any element in a list is in another list and return the first element found

后端 未结 6 635
陌清茗
陌清茗 2021-01-13 05:23

It is easy to check if an element of a list is in another list using any():

any(elem in list2 for elem in list1)

but is there

6条回答
  •  深忆病人
    2021-01-13 06:10

    Use sets: https://docs.python.org/2/library/sets.html

    result = set(list1) & set(list2)
    

    if you want to make it a conditional like any:

    if (set(list1) & set(list2)):
        do something
    

提交回复
热议问题