Lavellen - 10 months ago 29

Python Question

I don't have a code for this because I have no idea how to do it, and couldn't find much help on Google.

Is there a way to find if the same indexes on 2 lists are the same?

For example:

`x_list = [1, 2, 3, 4, 5]`

y_list = [1, 2, A, B, 5]

I want to know whether the first index of X is the same as the first index of Y, the second index of X is the same as the second index of Y, etc. I could do it like:

`x_list[0] == y_list[0]`

But need an infinite solution.

Answer

You can always use list comprehensions:

`[True if i == j else False for i,j in zip(x_list, y_list)]`

.
You can also check less explicit answer by Moses Koledoye, where `True if i == j else False`

is juzt `i == j`

zip function will combine your lists like [(1, 1), (2, 2), (3, 'A'), (4, 'B'), (5, 5)]

Result: `[True, True, False, False, True]`

Also suggest to use izip from itertools if you use Python2.