Open In App

How to Compare Two Iterators in Python

Python iterators are powerful tools for traversing through sequences of elements efficiently. Sometimes, you may need to compare two iterators to determine their equality or to find their differences. In this article, we will explore different approaches to compare two iterators in Python.

Compare Two Iterators In Python

Below are the ways to compare two iterators in Python:



Compare Two Iterators Using all and zip functions

In this example, we use a Python function approach1Fn() that uses the all function and the zip function to compare elements pairwise from two iterators (iter1 and iter2). The function returns True if all corresponding elements are equal and False otherwise. The example usage demonstrates comparing two iterators, iter1, and iter2, returning False as the fourth elements differ.




def approach1Fn(iterator1, iterator2):
    return all(x == y for x, y in zip(iterator1, iterator2))
 
# data
list1 = [1, 2, 3, 4]
list2 = [1, 2, 3, 4]
iter1 = iter(list1)
iter2 = iter(list2)
print(approach1Fn(iter1, iter2))

Output

True

Compare Two Iterators Using itertools.zip_longest

In this example, we are using the itertools.zip_longest function to compare elements pairwise from two iterators (iter1 and iter2). The approach2Fn() function returns True if all corresponding elements are equal, and it uses zip_longest to handle cases where the iterators are of different lengths by filling the shorter one with a specified fillvalue (defaulting to None). The example compares two iterators, iter1, and iter2, and returns False since the fourth elements differ.




from itertools import zip_longest
def approach2Fn(iterator1, iterator2):
    return all(x == y for x, y in zip_longest(iterator1, iterator2))
# data
list1 = [1, 2, 3, 4]
list2 = [1, 2, 3, 5]
iter1 = iter(list1)
iter2 = iter(list2)
print(approach2Fn(iter1, iter2))

Output
False

Compare Two Iterators Using itertools.tee and all

In this example, we are using the itertools.tee function to create two independent copies of the input iterators (iter1 and iter2). The approach3Fn() function then uses these copies to compare elements pairwise using the all function. The example compares two iterators, iter1, and iter2, and returns False since the fourth elements differ.




from itertools import tee
 
def compare_iterators(iterator1, iterator2):
    iter1_copy, iter2_copy = tee(iterator1), tee(iterator2)
    return all(x == y for x, y in zip(iter1_copy, iter2_copy))
 
# Example usage:
list1 = [1, 2, 3, 4]
list2 = [1, 2, 3, 5]
iter1 = iter(list1)
iter2 = iter(list2)
 
print(compare_iterators(iter1, iter2))

Output
False

Compare Two Iterators Using map and operator.eq

In this example, we are using the map function along with the operator.eq (equality operator) to compare elements pairwise from two iterators (iter1 and iter2). The approach4Fn() function returns True if all corresponding elements are equal. The example compares two iterators, iter1, and iter2, and returns False since the fourth elements differ.




import operator
def approach4Fn(iterator1, iterator2):
    return all(map(operator.eq, iterator1, iterator2))
# data
list1 = [1, 2, 3, 4]
list2 = [1, 2, 3, 5]
iter1 = iter(list1)
iter2 = iter(list2)
 
print(approach4Fn(iter1, iter2))

Output
False

Article Tags :