That might be a stupid question but have you tried vectorizing? Also, I haven't really understood why you need the second loop below. And isn't ``label`` a string?