This question already has an answer here:
- How can I compare two lists in python and return matches 16 answers
I have 2 lists, and I want to match each item with related index. what is the formula ? I have used set but it does not consider specific index.
list1 = [1 , 2 , 3, 5, 8]
list2 = [2 , 2 , 8, 5, 1]
out_put= [2 , 5]
You may use zip
to filter the same elements at each index of both the lists as:
>>> list1 = [1 , 2 , 3, 5, 8]
>>> list2 = [2 , 2 , 8, 5, 1]
>>> [i for i, j in zip(list1, list2) if i==j]
[2, 5]