1
a = [1, 2, 3]
b = [4, 5, 1]

I need to check if one (or more) elements are common in both arrays.

masroore
  • 9,668
  • 3
  • 23
  • 28

2 Answers2

5

Check the intersections of the sets and get the boolean:

>>> bool(set(a) & set(b))
True
>>> 

Or not not:

>>> not not (set(a) & set(b))
True
>>> 

Or just with any:

>>> any(i in b for i in a)
True
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

this is simple intersection concept which we used to learn in maths well here I have tried to demonstrate the function which take common from two list and return new list with common elements

    def intersection(lst1, lst2):
        lst3 = [value for value in lst1 if value in lst2]
        return lst3
    
    
    a = [1, 2, 3]
    b = [4, 5, 1]
    
    print(intersection(a,b))
ashhad ullah
  • 116
  • 4