'How to accelerate character manipulation to "transpose" a list of strings

I am trying to accelerate a python code to transpose a list of words and recombine them along the new axis. Example:

['HELLO','THERE','WORLD']
becomes
['HTW','EHO','LER','LRL','OED']

I can do this with numpy quite easily, it is about 10x slower than what I show below in pure python. I was hoping that pure python accelerated with jit could provide significant increase in speed.

Here are the two fastest codes I've written in pure python:

A = ['HELLO','THERE','WORLD']

def foo(A):
    for j in range(len(A)):
        if j==0:
            B=[_ for _ in A[0]]
            n=range(len(A[0]))
        else:
            for i in n:
                B[i]=B[i]+A[j][i]
    return B
foo(A)

def bar(A):
    return [''.join(_) for _ in zip(*(A))])
bar(A)

foo() runs in about 2.5 µsec on my machine. Using a ''.join() statement instead of the concat + slows it down by about 1 µsec. I thought maybe using jit would accelerate this but it slows foo down to 110 µsec. bar() runs at around 1.2 µsec, and is the best option I've found.

I'm quite curious if anyone can further accelerate this process while staying within python?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source