Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why garbage collection act weird this in Jupyter Notebook?

why second loop don’t print any thing?!

z_rng = zip(range(10), range(10))

# this loop is run and print
for i,j in z_rng:
    print(i,j)

# this loop is not run and don't print anything
for l, k in z_rng:
    print(l, k)

Or when print as list again loop don’t run.

z_rng = zip(range(10), range(10))

print(list(z_rng))

# this loop is not run and don't print anything
for l, k in z_rng:
    print(l, k)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Iterators can only be iterated once because they generate the values on the fly (they are not stored inside the iterator). The garbage collector isn’t the cause. You can create two iterators assigning two variables in one line.

You can check this question for further information.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading