You should save it as tuple (ts, words[i+1]) instead of converting to string. That way you avoid splitting it later in line 231.