Skip to content
Advertisement

To find words starting with vowels

I am trying to pickout the words that start with vowels from given list. I am having issue when the starting letter of word is a capital vowel. I am trying below code, can anyone explain why I am not getting desired output.

l=["wood", "old", 'Apple', 'big','item', 'euphoria']
p=[]
for i in l:
    if i[0] in('aeiou' or 'AEIOU'):
        p.append(i)
print(p)

Advertisement

Answer

The expression ('aeiou' or 'AEIOU') evaluates to just 'aeiou'. Thus you end up testing for lower case vowels only. For the reason, see How do “and” and “or” act with non-boolean values?

Instead, you wanted to say

if i[0] in 'aeiou' or i[0] in 'AEIOU':

or

if i[0] in 'aeiouAEIOU':

or

if i.lower() in 'aeiou':
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement