Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Lists Disemvowel

Kiran P
PLUS
Kiran P
Courses Plus Student 919 Points

Please review the code to check why Python is skipping the consonants as soon as it hits the first vowel in word.

Please review the code to check why Python is skipping the consonants as soon as it hits the first vowel in word.

disemvowel.py
def disemvowel(word):
    print("the word is {}".format(word))
    word = list(word)
    print("the word is {}".format(word))
    print("\n")
    vowels = ['a','e','i','o','u']
    temp = None
    print("The temp value is {}".format(temp))
    for mem in word:
        print(mem)
        print("The mem is {}".format(mem))
        print("\n")
        if mem.lower() in vowels:
            print("{} is a vowel".format(mem))
            word.remove(mem)
            print("{} removed".format(mem))
        else:
            if temp == None:
                temp = mem
                print("The value of temp is {}".format(temp))
                print("\n")
            else:
                temp += mem
                print("The value of temp is {}".format(temp))
                print("\n")
    word = temp
    return word

disemvowel('DytEQi')

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The problem with using the remove is that you're changing the thing you're iterating over. Essentially, it's messing up the indexing of the iterable. Every time you remove something, the following letter (whatever it is) will not be checked.

I've posted a couple of answers and threads on this. Take a look:

On the first, one I give a step by step account of what is happening at every iteration, but you will have to scroll down to the bottom of the thread to read it.

Hope this helps! :sparkles:

Kiran P
Kiran P
Courses Plus Student 919 Points

I copied word list to a temp variable and iterated on word list and removed the vowels in temp, but Python still skips the letter after removing one.

I tried the copy(), that worked.

Could you explain why Python is skipping again even though I'm removing the items in temp, a copy of word list?

Kiran P
Kiran P
Courses Plus Student 919 Points
def disemvowel(word):
    print("the word is {}".format(word))
    word = list(word)
    temp = word
    print("The temp is {}".format(temp))
    print("the word is {}".format(word))
    vowels = ['a','e','i','o','u']
    print("The temp value is {}".format(temp))
    for mem in word:
        print("The mem is {}".format(mem))
        if mem.lower() in vowels:
            print("{} is a vowel".format(mem))
            temp.remove(mem)
            print("{} removed".format(mem))
    word = "".join(temp)
    return word
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Kiran P Ah ha! the problem with this version of your code is that you didn't make a copy but rather a reference to the original list.

Take a look at this line:

temp = word

This tells Python that anything you do to temp, you also want to do to word, which is exactly the thing we're trying to avoid. To make a copy, you could change that line to:

temp = word[:]

This makes a copy of the word and stores it into temp.

I made an example for you to run in workspaces so you can see the difference between manipulating a copy and manipulating a reference.

Try running this in workspaces and take a look at what prints out:

fruit_list = ["apple", "banana", "cherry", "orange"]
new_list = fruit_list
fruit_list_copy = fruit_list[:]

print(new_list)
new_list.remove("apple")
# We only said to remove apple from new_list, but check out what is in the original fruit_list now
print(fruit_list)

print(fruit_list_copy)
fruit_list_copy.remove("banana")
#banana will be removed from here
print(fruit_list_copy)
#but not here
print(fruit_list)

Hope this helps! :sparkles: