title: Iterating and Updating the list in python
body: <blockquote>
    <p>I am not able to understand why the following code goes in indefinite
    loop(when i am not using the copy list)</p>
  </blockquote>
  
  <pre><code>list = ["Mohit","kumar","sffsfshfsd"]
  for w in list:
      if(len(w)) &gt; 5:
          list.insert(0,w)
      print("inside loop")
  
  print(list)  
  </code></pre>
  
  <p>The Above code prints <strong>inside loop</strong> indefinitely.</p>
  
  <p>Now if in place of the list, i use a copy list like below works fine.</p>
  
  <pre><code>list = ["mohit","kumar","sffffgssddf"]
  
  for w in list[:]:
      if len(w) &gt; 5:
          list.insert(0,w)
      print("inside loop")
  
  print(list)  
  </code></pre>
  
  <blockquote>
    <p>Now i have read in the python documentation that this is the behavior
    i will get but i want to understand the reason behind it. Thanks in
    advance.</p>
  </blockquote>