To find positions in which items with the same index differ in a pair of lists:
a = [1, 2, 3, 4] b = [1, 20, 30] indices_uniq = [i for i, (x, y) in enumerate(zip(a, b)) if x != y] # -> [1, 2]
Result [1, 2] as the second and third elements are different. Only i < min(len(a), len(b)) itertools.zip_longest(a, b, fill_value=sentinel) are itertools.zip_longest(a, b, fill_value=sentinel) , since there is no match for the shorter list of elements (otherwise, itertools.zip_longest(a, b, fill_value=sentinel) be used instead of zip(a,b) ).
For the literal input in the question, the result is [0,1,2] , that is, all elements are distinguished in pairs in the given lists. If you want to compare the input as a sequence of words separated by a space, you first need to normalize the input ( get_words() ), and then compare the corresponding words ( unequal_indicies(a, b) ):
def get_words(chunks): return (word for chunk in chunks for word in chunk.split()) def unequal_indices(a, b): return (i for i, (x, y) in enumerate(zip(a, b)) if x != y) a = ["120 devon 15.61", "126 korn05000000000000 12.96", "128 itswat 20.58"] b = ["120", "devon", "15.61", "126", "korn05", "12.96", "128", "itswat", "20.58"] print(list(unequal_indices(get_words(a), get_words(b)))) # -> [4]
In this case (when comparing by words) the result [4] , that is, only the fifth words in a given pair of texts differ.
In tests , you can use self.assertEqual(a, b) , which will show the differences between lists (Python 3), if any:
Traceback (most recent call last): File "./__main__.py", line 13, in test self.assertEqual(a, b) AssertionError: Lists differ: [1, 2, 3, 4] != [1, 20, 30] First differing element 1: 2 20 First list contains 1 additional elements. First extra element 3: 4 - [1, 2, 3, 4] ? ^^^ + [1, 20, 30] ? + ^
To compare sequences containing text, you can difflib to try :
>>> print(*difflib.ndiff(a, b), sep='\n') - 120 devon 15.61 - 126 korn05000000000000 12.96 - 128 itswat 20.58 + 120 + devon + 15.61 + 126 + korn05 + 12.96 + 128 + itswat + 20.58
The output shows that all elements are different.
If you normalize input, then only one word is expected to be different:
>>> print(*difflib.ndiff(list(get_words(a)), list(get_words(b))), sep='\n') 120 devon 15.61 126 - korn05000000000000 + korn05 12.96 128 itswat 20.58