Suppose I have a list of iterators:
iterators = [ iter([2, 3, 4]), iter([7, 9]), iter([-1, -2, -3, -4]) ]
How can I create a new iterator using the above iterators such that at each step it will randomly select one of the iterators present in the list (which are not yet finished) and output the next element from that? It should keep outputting elements until all the iterators have finished.
So, for example, if the new iterator is it_combined
, I may get the following output when trying to iterate on it
>>> for it in it_combined:
... print(it, end=" ")
...
2 7 3 -1 4 -2 -3 -4 9