Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two lists on Python

Tags:

python

list

I need help comparing two lists and returning the indices that they don't match.

a = [0, 1, 1, 0, 0, 0, 1, 0, 1]
b = [0, 1, 1, 0, 1, 0, 1, 0, 0]

indices 4 and 8 don't match and i need to return that as a list [4,8]

I've tried a few methods but they haven't worked for me.

like image 897
dfairch Avatar asked Dec 14 '25 02:12

dfairch


1 Answers

Use zip to iterate over both lists at the same time and enumerate to get the indices during iteration, and write a list comprehension that filters out the indices where the list values don't match:

>>> [i for i, (x, y) in enumerate(zip(a, b)) if x != y]
[4, 8]
like image 115
Aran-Fey Avatar answered Dec 16 '25 19:12

Aran-Fey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!