Python loops are slow as compared to underneath c loops. If you cannot get rid to python loop may be use cython there for that specific loop. I agree with you sometimes it’s hard to get what you want from the builtin functions. I hope it helps