Why is the output 0 1 2 and not 0 1 2 0 1 2? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is the output 0 1 2 and not 0 1 2 0 1 2?

x=(for i in range(3)) for i in x: print (i) for i in x: print (i) Why is the output of the code 0 1 2 instead of 0 1 2 0 1 2. And if x is a list why is the output 0 1 2 0 1 2.

17th Feb 2018, 12:12 PM
Shubham Anand
Shubham Anand - avatar
1 Answer
+ 10
typo you meant to write x=(i for i in range(3)) The reason is that the () makes it a generator and not a list. You exhausted the generator with your first for i in x: to get a list write x = [ i for i in range(3)] [] createss a list () creates a generator See https://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehension
17th Feb 2018, 12:47 PM
Louis
Louis - avatar