title: Why "for i in q" makes the result different from "for i in range(len(q))"?
body: <p>I have code for calculating max depth using BFS, which works fine, but I couldn't figure out why it fails if I replaced <code>for i in range(len(q)):</code> with <code>for i in q:</code>.</p>
  <p>The code below works correctly and passes all tests:</p>
  <pre><code>    def maxDepth(self, root: Optional[TreeNode]) -&gt; int:
          if not root: return 0
          
          q = list([root])
          level = 0
          while q:
              for i in range(len(q)):
                  node = q.pop(0)
                  if node.left:
                      q.append(node.left)
                  if node.right:
                      q.append(node.right)
              level += 1
          return level 
  </code></pre>
  <p>But the code below with <code>for i in q</code> fails some tests:</p>
  <pre><code>    def maxDepth(self, root: Optional[TreeNode]) -&gt; int:
          if not root: return 0
          
          q = list([root])
          level = 0
          while q:
              for i in q:                     # the only difference
                  node = q.pop(0)
                  if node.left:
                      q.append(node.left)
                  if node.right:
                      q.append(node.right)
              level += 1
          return level 
  </code></pre>
  <p><code>for i in range(len(q)):</code> and <code>for i in q:</code> provide exactly the same number of iterations or do I misunderstand something? I don't understand why it would make any difference... Any ideas?</p>
  <p>Thank you.</p>