Kamg300 Kamg300 - 9 months ago 23
Python Question

Iterative if function

I am currently working on a python script which will take a string, convert it to a list, and then remove all of the vowels. It then prints out the final result as a string. However, the iterative function I am using is outputting every result, whether it is a vowel or not. I have tried reversing and only keeping the consonants, but to no avail. Below is the code I was using.

string = input("#: ")
new_list = []

for x in list(string):
if [x.lower() == y for y in list("aeiou")]:
global new_list


Any insight would be appreciated.


This line:

if [x.lower() == y for y in list("aeiou")]

creates a list with five elements (each of which is True or False). It checks if this list is true. Any non-empty list is true, so the if condition is satisfied.

What you mean is:

# does x.lower() equal y for any y in my list of vowels?
if any(x.lower()==y for y in list("aeiou"):

or more succinctly:

if x.lower() in "aeiou":

If you want to check if a letter is not a vowel, it would be this:

if x.lower() not in "aeiou":