Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
For each, say whether it is an iterable that is not an iterator, or an iterator, and whether it can be looped over twice.
[1, 2, 3]iter([1, 2, 3])"abc"range(5)(n for n in range(3))Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. An iterator is what iter returns, and it is used up after one pass. An iterable can give a fresh iterator each time.
iter([1, 2, 3]): an iterator. Once.range: an iterable. Many times.Check. Try list(x) twice on each. The iterators give an empty list the second time.
Answer: iterable (many), iterator (once), iterable (many), iterable (many), iterator (once), iterator (once).
Log in to join the discussion.
Loading…