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

valerakushnir
valerakushnir
2,265 Points

Need a walkthrough for disemvowel exercise

I am stuck on this exercise. In general, i get stuck on functions as soon as there is an argument introduced so I really need some help as this is such a core concept.

  1. We have def disemvowel(word) function. Word is an argument in this case, so why aren't we required to call this function and provide what that word is in the end: disemvowel('ldskjflsfoaohknka') as an example. I am struggling here becuse it feels like an imcomplete function. I will take the word that we give as an argument, check for vowels, and then print it without them.

  2. I get lost when I get to for i in word. When a write a for statement that includes an argument from the function, will it always be in the end of a for statement?

  3. If i is a variable that we don't have defined anywhere, howcome we can do that? I thought variables have to be defined?

  4. Howcome .remove is not working here? I feel like this should be very simple.

disemvowel.py
def disemvowel(word):
    vowels = "a, e, i, o, u, A, E, I, O, U"

    for i in word:
        if i in vowels:
            word = word.remove(vowels)

    return word

1 Answer

Christopher Shaw
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 Points

You are on the right track, but remember, strings are immutable, so there is no remove function.

So as you loop though the word, if the letter is NOT IN vowels, then add it to a new string.

def disemvowel(word):
    newword = ''
    vowels = 'aieou'
    for letter in word:
        if letter.lower() not in vowels:
            newword += letter
    return(newword)
Ryan Hatter
Ryan Hatter
7,348 Points

Also, i actually is a variable.

For loops allow you to declare variables in a different way. So, you declared the variable i and limited the scope of i to the for loop.

See https://stackoverflow.com/questions/3559559/how-to-delete-a-character-from-a-string-using-python

And "The Python for loop starts with the keyword "for" followed by an arbitrary variable name, which will hold the values of the following sequence object, which is stepped through." www.python-course.eu/python3_for_loop.php

valerakushnir
valerakushnir
2,265 Points

ah, I see. Thank you so much! Also, now i get it what '' means (empty list).