My Intuitive Response for Removing Vowels is Not Working


Below is my approach for removing vowels in a string but I'm getting a syntax for my 'del' function. I could have sworn it's written correctly. Any thoughts? I thought I may have been able to get through this exercise without help. Maybe the next one i suppose!


del is a global function that does not belong to a particular special class.


The method counterpart that is a member of the list class, is list.remove() although it comes with a gotcha.


If I replace the del() functions with remove(), I get an error stating 'String indices must be integers, not str"
Looking through my code, I don't see where this would come into play.


Yep, that's another can of worms. Your previous attempt was simply using a method that did not exist.

list.del()    # non-existent method.

del(x[i])     # correct form of the expression

i is a string, not a number.

for i in x:
    if i == 'a' or i == 'A':

However, this can cause the problem of skipping since the iterable is mutated, shortening its length and shifting the next value into the current position, which has already been examined. Therefore it gets skipped over.

The same issue will arise using del(). There is a cure, though. Can you reason it out?


I will need to save this problem for tomorrow but I'll give it some thought.


Ok, so I'm trying to understand what you're telling me. When you say 'Mutated', you are referring to the fact that i is no longer a number but a string correct? By 'shortening its length', are you still referring to i or the string entered for the parameter?


The list is mutated, not i, and it is shorter with each mutation.


After giving this some more thought, If i give text a string (Say: cat). then by writing
for i in text

'i' will go into 'text" and treat 'cat' as the only element in 'text'. So my question then becomes: Do I need to find a way to create a sub-index to text[0]? I hope my question makes sense. If not then I will try to articulate better.


text = 'commendable'

for i in text:
    if i in 'aeiouAEIOU':

Right there we are in trouble because we cannot remove a letter from a string. Okay, so we make the string into a list...

text = list('commendable')

for i in text:
    if i in 'aeiouAEIOU':

print text                # ['c', 'm', 'm', 'n', 'd', 'b', 'l']

This looks like it works, and in the particular instance it does. Why? Because there are no two vowels side by side.

text = list('mountain')

for i in text:
    if i in 'aeiouAEIOU':

print text                # ['m', 'u', 'n', 't', 'i', 'n']

Now we see there are two vowels remaining.

When the o is removed, the u slides into its place, however that position has already been examined so the u is skipped in the next iteration. The same thing happens again when the a is removed. The i slips into its vacancy and the next letter to be examined is the n.

We can review a possible cure, using two objects. One to iterate, the other to mutate.

text = 'mountain'
temp = list(text)

for i in text:
    if i in 'aeiouAEIOU':

print ''.join(temp)       # mntn

And going to more extremes,

text = 'mountain'
temp = list(text)

for i in 'aeiouAEIOU':
    while i in temp

print ''.join(temp)       # mntn


Ok, this is starting to make more sense. My confusion seems to be knowing the difference from searching through a string and searching through a list. We need to make the string a list because we can't remove a character from a string unless that string is part of a list.

Also, I'm not sure if i was told about the list() function or if it has been a while that I forgot about it.


Both are ordered and iterable in exactly the same way, from index 0 to index len(object) -1, which is why a range iterable does not include the end point.

Eg. Starting at zero by default, it does have the count of 10, though.

list(range(10))  =>  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Above we iterate over the unchanging string so we examine every character in the string without skipping any. Changes we make in the list have no effect on the loop index.

print text    # mountain
print temp    # mntn


Thank you for helping me work through this problem mtf. I'm having a lot more luck with the next subsection concerning the lists.


Definitely work out some raw algorithms that do not resort to built-in functions or methods. That is the key purpose of this module of exercises. There are step by step approaches that may not be elegant, but that are infinitely important to have in our repertoire.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.