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>Specific index works like a charm for all indices <strong>up to the size of your list</strong>. The problem you're running into here is that when you remove an element from a list, you shrink it, the size is reduced by 1 every time you pop.</p>

<p>Say you have a list of 3 elements, <code>l = ["a","b","c"]</code>
You pop the first one <code>l.pop(0)</code>, that will return <code>"a"</code> but it will also modify the list so that now <code>l</code> is equal to <code>["b","c"]</code>.
If, instead, you had popped the last item with <code>l.pop(2)</code> or <code>l.pop(-1)</code> (because Python lets you count elements from the last one as well, so -1 is always the last element of the list), you would have gotten <code>"c"</code> and the list <code>l</code> would have become <code>["a","b"]</code>.
Notice that in both cases, the list has shrunk and has only two elements left, so you will not be able to pop the element number <code>2</code> now, since there is no such thing.</p>

<p>If you want to read an element instead of removing it from the list, use the <code>myList[elementIndex]</code> syntax. For instance, in your example, <code>nameList[6]</code> will return <code>"n7"</code> but it will not modify the list at all.</p>

python