0

learning Python, and for some reason I can't get the python remove function to work. It works when I'm testing it interactively in Python in the console but not when I write a script. Please help me out! it turns the input into a list but doesn't remove the vowels.

print("\nVowel Removal")
print("Enter a word to have the vowel removed.")
word_input = input("> ")
word_input = list(word_input)

vowels = list('aeiou')
output = []

while True:
    try:
        word_input.remove(vowels)
    except:
        print("You must enter a word.")
        break

print(word_input)
2
  • "It works when I'm testing it interactively in Python in the console but not when I write a script." But how are you testing it in the Python console? Commented Jun 24, 2015 at 7:26
  • 1
    word_input = list(word_input) vowels = list('aeiou') word_input.remove(vowels) this have no sense. what exactly you try to remove? Commented Jun 24, 2015 at 7:29

1 Answer 1

3

Here you have:

word_input = list(word_input)

So word_input is a list of strings (in particular of characters). vowels is:

vowels = list('aeiou')

i.e. an other list of strings.

You do:

word_input.remove(vowels)

which always fails because vowels is a list of strings and word_input only contains strings. remove removes a single element. It does not remove all elements contained in the argument. See the error message:

In [1]: vowels = list('aeiou')

In [2]: vowels.remove(vowels)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6dd10b35de83> in <module>()
----> 1 vowels.remove(vowels)

ValueError: list.remove(x): x not in list

Note that it says: list.remove(x): x not in list so the argument to remove should be an element of the list, not a list of elements to remove.

You have to do:

for vowel in vowels:
    word_input.remove(vowel)

to remove all vowels. Moreover remove only removes the first occurrence of the element, so you may have to repeatedly call remove to remove all occurrences of a vowel.

Note: to remove vowels from a string you could simply use:

the_string.translate(dict.fromkeys(map(ord, vowels)))

as in:

In [1]: the_string = 'Here is some text with vowels'
   ...: vowels = 'aeiou'
   ...: 

In [2]: the_string.translate(dict.fromkeys(map(ord, vowels)))
Out[2]: 'Hr s sm txt wth vwls'

Or if you want to use those lists:

result = []
# vowels = set('aeiou') may be faster than using a list
for char in word_input:
    if char not in vowels:
        result.append(char)
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the great answer! I will accept it as the correct answer in a couple of minutes when it lets me. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.