python split string and pick substring
<p>I am having a lot of trouble with the list returned by the split function in python.  I am probably being incredibly dense.</p>

<p>My python code is as follows</p>

<pre><code>str = "XXX:YYY:ZZZ"
var+=1
ins = str.split(':')
print ins
</code></pre>

<p>When the script runs it returns the following result</p>

<pre><code>['XXX','YYY','ZZZ']
</code></pre>

<p>What i am struggling to do is pull out the string contained the second string in the list.  I would have thought the following code at the end of the python code would work.</p>

<pre><code>print ins[1]
</code></pre>

<p>but when i run it i get the following error</p>

<pre><code>IndexError: list index out of range
</code></pre>

<p>Any help would be much appreciated</p>

<p>full code</p>

<pre><code>import time
ser = serial.Serial("COM3", 9600)
print ser
time.sleep(3)
print "Sending serial data"
var = 0
while var&lt;=10:
    str = ser.readline()
    print str
    var+=1
    ins = str.split(':')
    print ins
    print ins[0]
    if (str.split(':')=='end\n'):
        break
if(ser.isOpen()):
    print "Serial connection is still open."
    ser.close();
    print "Serial connectionnow terminated."
</code></pre>

<p>This returns</p>

<pre><code>Serial&lt;id=0x2a7fc50, open=True&gt;(port='COM3', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
Sending serial data
Program Initiated

['Program Initiated\n']
Program Initiated

0:addsd:1

['0', 'addsd', '1\n']
0
1:addsd:2

['1', 'addsd', '2\n']
1
2:end:2

['2', 'end', '2\n']
2
</code></pre>
