I have a string containing around 1 million sets of float/int coords, i.e.:    '5.06433685685, 0.32574574576, 2.3467345584, 1,,,'They are all in one large string and only the first 3 values are used. The are separated by 3 commas. The code i'm using right now to read through them and assign them to a list is below:    def getValues(s):        output = []        while s:            v1, v2, v3, _, _, _, s = s.split(',', 6)            output.append(%s %s %s % (v1.strip(), v2.strip(), v3.strip()))                 return output    coords = getValues(tempString)The end results need to be a list of the three floats in each set separated by a space, i.e.:     ['5.06433685685 0.32574574576 2.3467345584']The code I defined above does work for what I want, it just takes much longer than I would like. Does anyone have any opinions or ways I could speed it up? I would like to stay away from external modules/modules i have to download.Btw I don't think it's my computer slowing it down, 32gb ram and 350mb/s write speed, the program that creates the original string does it in about 20 secs, while my code above gets that same string, extracts the 3 values in around 30mins to an hour.P.s. Using python 2.6 if it mattersEDIT:Tried replacing the while loop with a for loop as I did some reading and it stated for loops were faster, it might have shaved off an extra minute but still slow, new code:    def getValues(s, ids):        output = []        for x in range(len(ids)):            v1, v2, v3, _, _, _, s = s.split(',', 6)            output.append(%s %s %s % (v1.strip(), v2.strip(), v3.strip()))                 return output
Python Optimizing/Speeding up the retrieval of values in a large string
python;optimization
How about this?import randomdef build_string(n):    s = []    for i in range(n):        for j in range(3):            s.append(random.random())        for j in range(3):            s.append(random.randint(0, 10))    s = ','.join(map(str, s))+','    return sdef old_getvalues(s):    output = []    while s:        v1, v2, v3, _, _, _, s = s.split(',', 6)        output.append(%s %s %s % (v1.strip(), v2.strip(), v3.strip()))             return outputdef new_getvalues(s):    split = s.split(,)    while not split[-1].strip():        del split[-1]    outputs = [' '.join(split[6*i:6*i+3]) for i in range(len(split)//6)]    return outputsI get (using 2.7 here, but I get similar times on 2.6):In [13]: s = build_string(3)In [14]: sOut[14]: '0.872836834427,0.151510882542,0.746899728365,1,5,2,0.908901266489,0.92617820935,0.686859068595,1,0,1,0.0773422174111,0.874219587245,0.473976008481,7,9,2,'In [15]: old_getvalues(s)Out[15]: ['0.872836834427 0.151510882542 0.746899728365', '0.908901266489 0.92617820935 0.686859068595', '0.0773422174111 0.874219587245 0.473976008481']In [16]: new_getvalues(s)Out[16]: ['0.872836834427 0.151510882542 0.746899728365', '0.908901266489 0.92617820935 0.686859068595', '0.0773422174111 0.874219587245 0.473976008481']In [17]: s = build_string(10001)In [18]: old_getvalues(s) == new_getvalues(s)Out[18]: Trueand times of1000 old 0.00571918487549 new 0.001165866851812000 old 0.0169730186462 new 0.001925945281984000 old 0.0541620254517 new 0.003877878189098000 old 0.240834951401 new 0.0089380741119416000 old 3.2578599453 new 0.020954847335832000 old 16.0219330788 new 0.0443530082703at which point I got bored waiting for the original code to finish.  And it seems to work nicely on your full case, taking about 2s on my notebook:In [32]: time s = build_string(10**6)CPU times: user 13.05 s, sys: 0.43 s, total: 13.48 sWall time: 13.66 sIn [33]: len(s), s.count(',')Out[33]: (51271534, 6000000)In [34]: s[:200]Out[34]: '0.442266619899,0.54340551778,0.0973845441797,6,9,9,0.849183222984,0.557159614938,0.95352706538,10,7,2,0.658923388772,0.148814178924,0.553198811754,1,0,8,0.662939105945,0.343116945991,0.384742018719,9,'In [35]: time z = new_getvalues(s)CPU times: user 1.14 s, sys: 0.75 s, total: 1.89 sWall time: 1.89 sIn [36]: len(z)Out[36]: 1000000In [37]: z[:3]Out[37]: ['0.442266619899 0.54340551778 0.0973845441797', '0.849183222984 0.557159614938 0.95352706538', '0.658923388772 0.148814178924 0.553198811754']