I am trying to understand in which situations an iterable can be replaced with an iterator in Python 3.
More specifically, consider this example:
ml = [1,2,3,4]
iter_ml = iter(ml)
deq_ml = collections.deque(ml)
deq_iter_ml = collections.deque(iter_ml)
print(ml)
print(iter_ml)
print(deq_ml)
print(deq_iter_ml)
This produces the output
[1, 2, 3, 4]
<list_iterator object at 0x7f6ee8eef4c0>
deque([1, 2, 3, 4])
deque([1, 2, 3, 4])
If I check the documentation of deque
, it accepts an iterable as the first argument. But here when I provided an iterator over the iterable, that worked too
However, it didn't work when the iterator is given to print
Same is the confusion with islice
. It works with both iterables and iterators.
How to know if those can be used interchangeably?