DEV Community

tortellini
tortellini

Posted on

3 1

Careful when using Python Generators!

Yes, they are memory efficient!

  • Python generators may prove themselves extremely useful in cases when you have large amounts of data and are trying to feed it to an iterator. In short, generators return a lazy iterator that does not store their contents in memory. WOW! Great, sounds like you save yourself the trouble of running into a MemoryError, right?

But, they hide something...

nums = (x for x in range(10000000000000))
for num in nums:
   print(num, end=" ")
>>> 1,2,3,4....
for num in nums:
   print(num*2, end=" ")
# What do you think is the output of the above for loop?

Generators are hungry hungry!

  • When iterating over a generator, the elements at each location are essentially being "consumed" and "discarded".
  • In other words, if you try to iterate AGAIN over the generator, it will look like all of your elements vanished!

Answer to the code above:

  • You will see no numbers printed to the console.

Leave your comments below!

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay