title: Sort list of numbers doesn't work with for loop
body: <p>I have a list of numbers:</p>
  
  <pre><code>list = [2, 5, 4, 1, 3]
  </code></pre>
  
  <p>I want to sort it and iterate sorted list using for loop:</p>
  
  <pre><code>print list
  list.sort()
  print list
  for n in list:
      print list
      print n
      list.pop(0)
  </code></pre>
  
  <p>But the output doesn't look as expected:</p>
  
  <pre><code># before sort:
  [2, 5, 4, 1, 3]
  # after sort:
  [1, 2, 3, 4, 5]
  ## still everything ok
  # for loop:
  [1, 2, 3, 4, 5]
  1
  [2, 3, 4, 5]
  3
  [3, 4, 5]
  5
  ## what?
  </code></pre>
  
  <p>Why the for loop doesn't accept sorted list and works with the state before even though it returns the sorted list? Is there any hidden indexing, which <code>sort()</code> can't change? I'm new in Python and this is really weird behavior. What am I doing wrong?</p>
  
  <p>What I want to achieve:</p>
  
  <pre><code># before sort:
  [2, 5, 4, 1, 3]
  # after sort:
  [1, 2, 3, 4, 5]
  # for loop:
  [1, 2, 3, 4, 5]
  1
  [1, 2, 3, 4, 5]
  2
  [1, 2, 3, 4, 5]
  3
  [1, 2, 3, 4, 5]
  4
  [1, 2, 3, 4, 5]
  5
  </code></pre>
  
  <p><strong>Update:</strong> I need that pop there. My full code of this function:</p>
  
  <pre><code>inTemplate = [False]
  inLink = [False]
  regexNeeded = [False, True]
  # ...some if elif else skipped there
              # finds positions of brackets and sorts them
              brackets = [m.start() for m in re.finditer("}}",text)]
              brackets += [m.start() for m in re.finditer("{{",text)]
              brackets += [m.start() for m in re.finditer("]]",text)]
              brackets += [m.start() for m in re.finditer(r"\[{2}",text)]
              brackets.sort(key=int)
              # if any bracket in text
              if not brackets == []:
                  # for every bracket
                  for n in brackets:
                      # what bracket and what starts/ends with it
                      if text[n] == '{':
                          inTemplate.append(True)
                      elif text[n] == '[':
                          inLink.append(True)
                      elif text[n] == ']':
                          inLink.pop()
                      else:
                          if inTemplate[-1]:
                              inTemplate.pop()
                          # if ends template which doesn't started, then there is no need to regex
                          elif regexNeeded[-1]:
                              regexNeeded.pop()
                          # break if out of regexNeeded area
                          else:
                              break
                      brackets.pop(0)
                  # if for loop breaked
                  if not brackets == []:
                      # do some regex on that bracket, where for loop breaked
                      where = brackets[0]
                      before = text[:where]
                      before = re.sub(r'\s*$', r'\n', before)
                      after = text[where:]
                      if not regexNeeded[-1]:
                          after = re.sub(r'^}}\s*', r'}}\n', after)
                      text = before + after
  </code></pre>