title: How Python Generators Save Memory And Time?
body: <p>I'm working on Generators. I can't easily take in some informations I learnt.</p>
  <pre><code>y = []
  def x(a: int) -&gt; None:
      for x in range(a):
          y.append(x)
  
  def z(a):
      for i in range(a):
          yield i
  
  x(10**6)
  t = z(10**6)
  print(next(t))
  print(next(t))
  </code></pre>
  <p>Comparing two code,</p>
  <p><strong>first code;</strong></p>
  <ul>
  <li>y is creating list object by setting []</li>
  <li>And in for loop y.append() function calls 10**6 times and do operations</li>
  <li>If I want to reach any value of y, y will go to heap memory and retrieve data I just requested</li>
  </ul>
  <p><strong>second code;</strong></p>
  <p>All values will yield when I call t = z(10**6)? or It will yield when I call next function I mean when I use next function it will go the generator and yield next value and get it from there? Does it remember where it left with a pointer? Does it delete the value after next function used I mean after calling print(next(t)) it will delete just after the operation?
  Thanks in advance!</p>