The to_list operator converts an observable sequence to a single item that contains all the items emitted by the source observable, wrapped in a list. The following figure shows the marble diagram of this operator:
Figure 9.25: The to_list operator
Its prototype is the following:
numbers = Observable.from_([1, 2, 3, 4])
numbers.to_list().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:
Only one item is received, containing the source sequence as a list.
There are several variants of this operator:
- to_set converts a sequence to a set
- to_dict converts a sequence to a dictionary