Python prints word
If anyone can help me with the Python code: If I enter a letter, how can I print all the words that start with this word?
+1
mizanur
source
to share
2 answers
print [word for word in words if word.startswith(letter)]
+2
Mariano
source
to share
There are many ways to do this, for example:
words = ["zwei", "peanuts", "were", "walking", "down", "the", "strasse"]
letter = "w"
output = [x for x in words if x[0] == letter]
The content output
will be:
['were', 'walking']
Some notes:
- If the code needs to be fast you should put the wordlist in some kind of tree.
- If you need more flexibility, you should create a regex to match
+1
csl
source
to share