The zip operator combines items emitted by several observables, one by one. Each time all the source observables emit one item, then these items are combined as another item emitted on the output observable. The following figure shows the marble diagram of this operator:
Figure 9.15: The zip operator
This operator can be used both as a method or a static method. Its prototype is the following:
Observable.zip(self, *args)
Observable.zip(cls, *args)
Here, several observables can be provided as args. The last parameter provided must be a function that combines the items of all the source observable items.
Here is an example of the zip operator:
numbers = Observable.from_([1, 2, 3, 4])
characters = Observable.from_(['a', 'b', 'c', 'd' ,'e'])
characters.zip(numbers, lambda c, n: "{}: {}".format(c, n)).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: