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

I'm getting the error "IndexError: list assignment index out of range" on the "disemvowel" code challenge. Why is this?

Is there a quicker or more concise way of doing this? I feel like I'm doing extra steps, but I can't think of another way to do it...

disemvowel.py
def disemvowel(word):
    list_word = list(word)
    index = 0
    for letter in list_word.copy():
        if 'a' or 'e' or 'i' or 'o' or 'u' in letter.lower:
            del list_word[index]
        index += 1
    word = ''.join(list_word)
    return word

1 Answer

Wade Williams
Wade Williams
24,476 Points

You have a good code smell that you're doing extra work and when you feel that way, it's good to look at the entire code base. Here are some thoughts:

  1. Python will naturally loop over each character when given a string in a for loop, so no need to turn your word into a list first.
  2. We don't want to check if a vowel is in a letter, but instead check if a letter is in vowels. Let's create a vowels variable that holds all vowels.
  3. We're returning a joined list, so lets create a variable that's an empty list and add all non-vowels to it, then returned the joined list.
def disemvowel(word):
    vowels = "aeiouAEIOU"
    result = []

    for letter in word:
        if letter not in vowels:
            result.append(letter)

    return "".join(result)

Thanks! This really helps! Do you know what was actually making the error occur in my version? I’m just wondering because I want to understand why it didn’t work so I can avoid doing it in the future. I was making it a list so that I could use the del function, not for the loop, but I see that your way works better. Thanks again!

Wade Williams
Wade Williams
24,476 Points

The reason you were getting a index error is because you were deleting letters from the list_word as you were looping and list_word was actually getting shorter as you went through each iteration and eventually you try to access an index value that exceeds the length of list_word.

Lets try disemvowel("Gabriel") for an example :

disemvowel("Gabriel")

list_word = ["G", "a", "b", "r", "i", "e", "l"]
index = 0

for letter in ["G", "a", "b", "r", "i", "e", "l"]
  if 'a' or 'e' or 'i' or 'o' or 'u' in letter.lower:
    del list_word[index]
  index += 1

# First Iteration
letter = "G"
index = 0
list_word = ["G", "a", "b", "r", "i", "e", "l"]


# Second Iteration
letter = "a"
index = 1
list_word = ["G", "a", "b", "r", "i", "e", "l"]
# Delete list_word[1] which is "a"
# Now list_word shrinks and all letters after "a" shift down by 1


# Third Iteration
letter = "b"
index = 2
list_word = ["G", "b", "r", "i", "e", "l"]


# Fourth Iteration
letter = "r"
index = 3
list_word = ["G", "b", "r", "i", "e", "l"]


# Fifth Iteration
letter = "i"
index = 4
list_word = ["G", "b", "r", "i", "e", "l"]
# Delete list_word[4] which is actually "e" not the letter "i" as expected


# Sixth Iteration
letter = "e"
index = 5
list_word = ["G", "b", "r", "i", "l"]
# Delete list_word[5] which gives you an index error because we deleted the "a" and "e" and list_word only goes up to index 4 now