What does the following code output? from itertools import islice g = (x**2 for x in range(100)) print(list(islice(g, 4)))

Python Developer Medium

Python Developer — Medium

What does the following code output? from itertools import islice g = (x**2 for x in range(100)) print(list(islice(g, 4)))

Key points

  • islice is used to slice elements from an iterable
  • The generator g generates squares of numbers
  • The range function generates numbers from 0 to 99
  • The list function converts the sliced elements into a list

Ready to go further?

Related questions