First common element from two lists

后端 未结 10 1092
执笔经年
执笔经年 2020-12-20 14:07
x = [8,2,3,4,5]
y = [6,3,7,2,1]

How to find out the first common element in two lists (in this case, \"2\") in a concise and elegant way? Any list

10条回答
  •  既然无缘
    2020-12-20 14:47

    def first_common_element(x,y):
        common = set(x).intersection(set(y))
        if common:
            return x[min([x.index(i)for i in common])]
    

提交回复
热议问题