Pop from empty list error ONLY on lists with even number of elements
<p>So this is just an exercise I'm doing for fun on codewars.com. The point is to take a string and pull it apart by taking the last character and adding it to a string, taking the first character and adding it to another string until you have either 1 (for a string with an odd number of letters) or 0 (for a string with even number of letters) characters left. Here's a link to the <a href="https://www.codewars.com/kata/popshift/train/python" rel="nofollow">challenge</a> if you're interested.</p>

<pre><code>def pop_shift(test):
    firstSol = []
    secondSol = []
    testList = list(test)
    while len(testList) != 1:
        firstSol.append(testList.pop())
        secondSol.append(testList.pop(0))
    return [''.join(firstSol), ''.join(secondSol), ''.join(testList)]
</code></pre>

<p>My code gives me the right result if the string has an odd number of characters:</p>

<pre><code>['erehtse', 'example', 't']
</code></pre>

<p>But with an even number of characters I get this error:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;pyshell#37&gt;", line 1, in &lt;module&gt;
    pop_shift("egrets")
  File "&lt;pyshell#35&gt;", line 6, in pop_shift
    firstSol.append(testList.pop())
IndexError: pop from empty list
</code></pre>

<p>I've looked through a bunch of questions involving the pop() method, but nothing sounded quite similar to this. I've also tested this with a variety of strings and looked into the documentation for the pop method. There must be something that I'm missing. Any pointers are appreciated. This is also my first question, so if there's anything else you'd like to see, please let me know.</p>

<p>Instead of <code>while len(testList) != 1</code>, you need: <code>while len(testList) &gt; 1</code>, since <code>len(testList)</code> will jump from <code>2</code> to <code>0</code> on "even" strings:</p>

<pre><code>def pop_shift(test):
    firstSol = []
    secondSol = []
    testList = list(test)
    while len(testList) &gt; 1:
        firstSol.append(testList.pop())
        secondSol.append(testList.pop(0))

    return [''.join(firstSol), ''.join(secondSol), ''.join(testList)]
</code></pre>

<p>Then:</p>

<pre><code>print(pop_shift("Monkeys"))
&gt; ['sye', 'Mon', 'k']

print(pop_shift("Monkey"))
&gt; ['yek', 'Mon', '']
</code></pre>

python list pop