Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!

PythonPr's tweet image. Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!

Answer B: [0, 1, 2], [] (i for i in range(3)) creates a generator, not a list! › First list(g): consumes generator [0,1,2] › Second list(g): generator exhausted [] For beginners: Generators are iterators, so they can be used one time only. Use [i for i in range(3)]…


B , Cause generator object is exhausted while using list(g) To get a new list , we have to generate the object again -- at first i also thought this is a simple comprehension but it is another way of creating generators in the python 😀


Answer is...B) [0, 1, 2], [] = first call consumes the generator, second one finds it empty. Generators can’t be reused 😎


nooo its a generator sry


A Python starts counting from 0, so it would count as 0 , 1 , 2. And the list() method is used to organize items to a list.


Output is B) [0, 1, 2], []. For More Reference =>>

KumarT00623760's tweet image. Output is B) [0, 1, 2], [].

For More Reference =>>

@grok what's the answer and why


B This is really intereting !!Thank you for sharing


This post is unavailable.

B) [0, 1, 2], []


United States Trends
Loading...

Something went wrong.


Something went wrong.