Search code examples
pythonstemming

Get the longest common string prefix of all words in a list


Is there a function that returns the (length of the) longest string that all values start with in a list:

["flexible","flexile","flexion","flexor"]

Should return: "flex" or 4

And for this: ["flexible","flexile","flexion","flexor","ape"], it should return an empty string or 0;


Solution

  • >>> import os
    >>> os.path.commonprefix(["flexible","flexile","flexion","flexor"])
    'flex'