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 (Retired) Lists Redux Disemvoweled

is my explanation of how this code works correct please?

I would love an explanation in English of how this code works. There is no explanation in the video that I can understand. I am particularly interested in whether it takes each of the state names in turn, and looks at it for each of the vowels in turn OR whether it takes each vowel in turn and then looks at each state name. If anyone has the time or inclination, you can read the notes I have made next to Kenneth's code to try to explain to myself what is going on. If you are able to correct where I am going wrong I would be much obliged.

ps: you will note that I very much i need of some pretty straightforward explanation of the code BECAUSE I am struggling with reading the syntax to understanding the linear processes being performed. If anyone has an alternative course, book, or course provider I am all ears: I am finding treehouse is not right for someone like me who is both learning how to code for the first time AND learning python for the first time. Thanks!

state_names = ["alabama", "california" , "oklahoma" , "florida"]
vowels = list("aeiou")
output = []

for state in state_names: # this says: 1) call each of the words in the list called state_names "state" 2) go through each state in the list state_names and do to each item the things that appear in the code below
    state_list = list(state.lower()) # this says 1) create a new list called state_list.2) The list comprises of each of the states from above 3) each of the states above are returned in lower case              
    for vowel in vowels: #this says 1) call each item in the list called vowels "vowel 2) go through each of those items and perform the task that appears below 
        while True: #this says while there is not a break
            try: 
                state_list.remove(vowel) #while there are no problems 1) go to state_list (which, remember is a list of all the states in lowercase) 2) take each of those states in turn 3) remove the vowels from them
            except: #when you are running the above while true on each of the vowels in turn, if you get an exception (which will happen once there are no longer any vowels to go through) then stop doing this thing
                break #this stops this piece of code on the vowel that is being taken, however it will then go back to line 8 and take the next vowel in turn and start the proccess on line 11 again.
    output.append("".join(state_list).capitalize()) # this 1) adds to a list called output the result of the result of a loop I cannot identify 2) it joins the various single values into a tring using the .join function 3) it capitalizes (though how it knows to capitalize the first letter I do not know)

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Woah, that's alot of comments there, I don't have time to go through 'em all; however, I can give you some pointers on this particular line, because you seem to misunderstand it a little bit.

state_list = list(state.lower())

The purposes of list(state.lower()) are twofold, first the .lower() method makes sure the state name is in all lower case, if it isn't already; and secondly, since state.lower() returns back a String, and by passing a string to the list() method, what this does is to split each LETTER of the String onto its own entity, and then put them into a list.

For example, first item in the state_names list is the String "alabama".

>>> list("alabama".lower())
['a', 'l', 'a', 'b', 'a', 'm', 'a']

Hope this helps you comprehend what this line does a little better, and you may want to adjust your understanding of the rest of the lines that follow.


For learning purpose, I believe Kenneth is writing the code this way to show the use of try ... except clause, and .remove() methods; if the use of these 2 isn't a requirement, there's a much simpler and easy-to-understanding solution.

state_name = ["Alabama", "California", "Oklahoma", "Florida"]
vowels = list('aeiou')
output = []

for state in state_name:
    disemvowels = ""                # empty String placeholder for all the non-vowel letters
    for letter in state.lower():
        if letter not in vowels:    # if that letter is NOT in the vowels list
            disemvowels += letter   # add the letter to disemvoweled
    output.append(disemvowels.capitalize())

print(output)

Lastly.

I am finding treehouse is not right for someone like me who is both learning how to code for the first time AND learning python for the first time.

That's not true, I do not believe there's another programming language that is as beginner-friendly as Python.

Hi William: thanks for your tip on Kenneth's code! It helped me enormously to understand what on earth it is doing! Secondly I am sure that your code is a better way: I am not really qualified to give an evaluation of it. Finally sorry you misunderstood. I wrote that TREEHOUSE is not working well as a newbie to both coding and python. You replied by contending that Python is the most beginner friendly: I am sure that you are right. I was not suggesting otherwise. My comment was on TREEHOUSE not PYTHON.
.

For those of you who like me were struggling to understand what this code does I have written in English what Kenneth has written in code.

Look at list of state names Take each state name in turn (for function) Take this state name and make it lower case (use the lower() function) Take the state name (which is a string) and turn it into a list of individual letters (using the list() function)

Once you have done that, enter into a new loop. This is where you: Now take each of the vowels in turn from a list of vowels. then go through this lists of individual letters made up up of the state name above. Remove from this list each of the vowels in turn.

Once you have done this for each state list, make a new list made up of the remnant consonants from the state name list. Capitalize this new list.

Then print the entire new list, of every state name without vowels.