LearnFrenzy
Sign in Take a free test

Iterators and Generators · Iterators and Generators MCQ · Question 14

Taking items from an infinite generator Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links