l=["carbon","cart","carrot"] l.sort(key=lambda x: len(x)) Common_Prefix,m="",1 for number in range(len(l[0])): scale=l[0][0:number+1] for word in l: if word.startswith(scale)==False: m=-1 break if m==-1: break Common_Prefix=scale print(word)