compare two lists python code example

Example 1: compare lists element wise python

[ x&y for (x,y) in zip(list_a, list_b)]

Example 2: how to compare two lists element by element in python and return matched element

>>> [i for i, j in zip(a, b) if i == j]
[5]

Example 3: diff 2 lists python

def get_diff(a: list,b: list) -> list:
    return list(set(a) ^ set(b))

Example 4: compare two list in python

>>> s = ['a','b','c']   
>>> f = ['a','b','d','c']  
>>> ss= set(s)  
>>> fs =set(f)  
>>> print ss.intersection(fs)   
   **set(['a', 'c', 'b'])**  
>>> print ss.union(fs)        
   **set(['a', 'c', 'b', 'd'])**  
>>> print ss.union(fs)  - ss.intersection(fs)   
   **set(['d'])**