My list has
a = [1,2,3,4,2,7,3,5,6,7]
b = [1,2,3,1,2,5,6,2,6,7]
I need to count if a[i]==b[i]
.
For the above example, the answer should be
6
Detail description of answer is
a[0]==b[0] (1==1)
a[1]==b[1] (2==2)
a[2]==b[0] (3==3)
a[4]==b[4] (2==2)
a[8]==b[8] (6==6)
a[9]==b[9] (7==7)
The len() is used to return total count of match. The enumerate function can be used to produce the key value pair for the list being it's index and value and we can store them using list comprehension. The len() is used to return total count of match.
Check Equality of Lists in Python Using the Equality == Operator. A straightforward way to check the equality of the two lists in Python is by using the equality == operator. When the equality == is used on the list type in Python, it returns True if the lists are equal and False if they are not.
Len() Method There is a built-in function called len() for getting the total number of items in a list, tuple, arrays, dictionary, etc. The len() method takes an argument where you may provide a list and it returns the length of the given list.
In a one-liner:
sum(x == y for x, y in zip(a, b))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With