The distinct operator returns only unique items from its source observable. The following figure shows its marble diagram:
Figure 9.11: The distinct operator
Its prototype is the following:
Observable.distinct(self, key_selector=None, comparer=None)
Here, key_selector is a function that returns a key from each item. The default value uses the item as a key. The comparer parameter is a function used to compare two keys. Its default value uses the equality operator. Both parameters are optional.
Here is an example of the distinct operator:
numbers = Observable.from_([1, 2, 1, 3, 3, 2, 4, 5])
numbers.distinct().subscribe(
on_next = lambda i: print("on_next {}".format(i)),
on_error = lambda e: print("on_error: {}".format(e)),
on_completed = lambda: print("on_completed")
)
This example gives the following result: