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

Anatolii Tcai
Anatolii Tcai
2,401 Points

How to solve this? How can I improve my code?

So for this task, I've come up with the code below. How can I improve it to get rid of the range? What should I do so the code is accepted?

disemvowel.py
#disemvowel

def disemvowel(word):
    letters = ("a","e","i","o","u")
    new_word = list(word.lower())
    word = ""
    for i in range(0,10):
        for letter in letters:
            try:
                new_word.remove(letter.lower())
            except ValueError:
                pass      
    return word.join(new_word)  

1 Answer

You can actually loop over lists and strings. The code below is perfectly valid:

my_list = [1, 2, 3, 4, 5]

for x in my_list:
    print(x)

# The loop above is the same as...

for x in range(len(my_list)):
    print(my_list[x])

# Another example with iterating over strings...

for x in 'abcdefg':
    print(x)

# I hope these examples help :)

I hope this helps! :grin:

:dizzy: ~Alex :dizzy: