Ned Batchelder is probably right. But for fun, here's a more efficient version of phimuemue responding with itertools .
import itertools strings = ['my_prefix_what_ever', 'my_prefix_what_so_ever', 'my_prefix_doesnt_matter'] def all_same(x): return all(x[0] == y for y in x) char_tuples = itertools.izip(*strings) prefix_tuples = itertools.takewhile(all_same, char_tuples) ''.join(x[0] for x in prefix_tuples)
As an insult to readability, here is a single-line version :)
>>> from itertools import takewhile, izip >>> ''.join(c[0] for c in takewhile(lambda x: all(x[0] == y for y in x), izip(*strings))) 'my_prefix_'
senderle Jul 16 '11 at 18:12 2011-07-16 18:12
source share