find common prefix in a list of words in python

To find the common prefix in a list of words, we can use the os.path.commonprefix() method. This method takes an iterable of strings and returns the longest common prefix of those strings.

Here's an example:

main.py
import os

words = ['apple', 'app', 'append', 'application']

prefix = os.path.commonprefix(words)

print("The common prefix is:", prefix)
139 chars
8 lines

Output: The common prefix is: app

In this example, we import the os module and create a list of words. We then call the os.path.commonprefix() method and pass in the list of words. The method returns the longest common prefix, which is 'app'.

gistlibby LogSnag