Question 14
Back to Exercise
How do you get the first 5 values of an infinite generator g?
- A g[:5]
- B list(itertools.islice(g, 5)) Correct
- C list(g)[:5]
- D len(g) - 5
Correct Answer: Option B
Generators cannot be sliced, and list(g) would never finish.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!