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

huyen nguyen
huyen nguyen
850 Points

can anyone help me with my code?

Please help me with the attached code what's the purpose of 'return word' at the end of the code?

disemvowel.py
def disemvowel(word):
    vowel=["a","e", "i", "o", "u"]
    new_list=[]
    for i in word:
        new_list.append(i.lower())
    for a in vowel:
        if a in new_list:
            new_list.remove(a)
    new_word="".join(new_list)
    return word

1 Answer

I think it should be return new_word, since that's the version (with removed vowels) we'd like to return. (In your version nothing happens, the function just gives back the input string.)

If your question is about the purpose of a return statement in general: when you call the function, it gives back (returns) a value, which can be worked with or stored in a variable - else your function could only do modifications on the input or on something else outside its scope (a so-called "side effect").

To further elaborate: when you call a function, the whole expression function(argument) evaluates to its return value (which is None by default, or the value given back by one of the return statements inside the function). Hence that you can pass in a function call as an argument to another function, like this: print(disemvowel("elephant")), since you actually pass in a value - it means the same as print(<return value of the function call disemvowel("elephant")>)

# Without a return statement inside the function, the return value would be the default `None`,
# so the print statement on the next line would also print `None`.

disemvoweled = disemvowel("elephant")
print(disemvoweled)  # prints lphnt
# or:
print(disemvowel("elephant"))  # also prints lphnt

(Sidenote: since strings are immutable in Python - you cannot modify them once created -, it is actually impossible to implement a version of this method which removes the vowels directly from the input string, without returning a new string.)