How To Find Out The Words Begin With Vowels In A List?
words = ['apple', 'orange', 'pear', 'milk', 'otter', 'snake', 'iguana', 'tiger', 'eagle'] vowel=[] for vowel in words: if vowel [0]=='a,e': words.append(vowel)
Solution 1:
words = ['apple', 'orange', 'pear', 'milk', 'otter', 'snake','iguana','tiger','eagle']
for word in words:
if word[0] in'aeiou':
print(word)
You can also use a list comprehension like this
words_starting_with_vowel = [word for word in words if word[0] in 'aeiou']
Solution 2:
Here is a one-liner answer with list comprehension:
>>> print [w for w in words if w[0] in'aeiou']
['apple', 'orange', 'otter', 'iguana', 'eagle']
Solution 3:
Good python reads almost like natural language:
vowel = 'a', 'e', 'i', 'o', 'u'
words = 'apple', 'orange', 'pear', 'milk', 'otter', 'snake', 'iguana', 'tiger', 'eagle'print [w for w in words if w.startswith(vowel)]
The problem with w[0]
solution is that it doesn't work with empty words (doesn't matter in this particular example, but important in real-life tasks like parsing user input).
Solution 4:
if vowel [0]=='a,e':
words.append(vowel)
You are appending it to the original list here. It should be your vowel
list.
words = ['apple', 'orange', 'pear', 'milk', 'otter', 'snake','iguana','tiger','eagle']
vowel=[]
for word in words:
if word[0] in "aeiou":
vowel.append(word)
print (vowel)
Using List comprehension
vowel = [word for word in words if word[0] in "aeiou"]
Using filter
vowel = filter(lambda x : x[0] in "aeiou",words)
Solution 5:
res = []
list_vowel = "aeiou"for sub in words:
flag = False# checking for begin char for ele in list_vowel:
if sub.startswith(ele):
flag = Truebreakif flag:
res.append(sub)
# printing result
list_vowel = str(res)
print(list_vowel)```
Post a Comment for "How To Find Out The Words Begin With Vowels In A List?"