deleting (POPing) specific elements from list
<p>I am doing book exercise on pop() function in Python (3.5). The instructions are to delete elements from the list using pop(). From the list below, I want to delete n1, n4, n5, n6, n7, n8, n9. Below code works, but very practical) and I don't understand why specific index works only up until [5]. With out using loops (I'm not there yet), what is the proper way of poping specific elements from the list? </p>

<pre><code>nameList = ['n1', 'n2', 'n3', 'n4', 'n5', 'n6', 'n7', 'n8', 'n9']
print('I can only invite two people to dinner...')

print('Sorry, but ', nameList.pop(0).title(), ' will not be invited to 
       dinner') 
print('Sorry, but ', nameList.pop(3).title(), ' will not be invited to
      dinner')
print('Sorry, but ', nameList.pop(4).title(), ' will not be invited to
      dinner') 
print('Sorry, but ', nameList.pop(5).title(), ' will not be invited to
      dinner') 
print('Sorry, but ', nameList.pop(-1).title(), ' will not be invited to
      dinner')
print('Sorry, but ', nameList.pop(-1).title(), ' will not be invited to
      dinner')
print('Sorry, but ', nameList.pop(-1).title(), ' will not be invited to
      dinner') 
</code></pre>

<p>Well, the length of nameList changes dynamically every time you use 'pop'.
So after you pop 4 elements(n1,n4,n5,n6), there are only 5 elements left in nameList. 
You can not use pop(5) anymore because the index is out of range at that time.</p>

python