how to compare elements in list python code example

Example 1: 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 2: python compare each item of one list

import itertools
for a, b in itertools.combinations(mylist, 2):
    compare(a, b)

Example 3: how to make every item compare the rest items of list in python

for i in range(len(mylist)):
    for j in range(i + 1, len(mylist)):
        compare(mylist[i], mylist[j])

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'])**