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

Need help! devowel.py

Guys, can somebody explain me in as much detail as possible what does state_list variable contain:

for state in state_names: state_list = list(state.lower())

I mean I don't understand what exactly is happening in here. We have a list state_names why do we need to use list() in here ?

Thanks, Frank

9 Answers

Josh Keenan
Josh Keenan
19,652 Points

That's what the loop after does, this bit of the loop just creates a list with the lower cased state.

statelist = ['a', 'l', 'a', 'b', 'a', 'm', 'a']     # <-- This is what the variable stores at first

  for vowel in vowels:        # <-- This loop is where it starts the comparisons, it goes over 'a' then 'e', 'i', 'o', 'u'
    while True:
      try:
        state_list.remove(vowel)          # <-- it removes the vowel from the letter wherever it appears
      except:
        break
  output.append(''.join(state_list).capitalize())       # <-- it puts the new word back together and adds it to the output
Josh Keenan
Josh Keenan
19,652 Points

state_list contains a set of strings, some States in the USA with capital letters at the start of each string so:

state_names = ['Alabama', 'California', 'Oklahoma', 'Florida']
vowels = list('aeiou')

This is what you get at the start, and we're trying to remove every vowel from each state, to do that we need to use the list of vowels which are all lower cased whereas the state names aren't all lower cased.

We create a new list of states decapitalized.

This gives python the ability to recognize the starting letters.

for state in state_names: 
  state_list = list(state.lower())

We then add the decapitalized state names to a new list in order to allow python to iterate over them!

Post again if you need me to explain more or need any help!

I understand the decapitalization part; list looks like that state_list = ['alabama', 'california', 'oklahoma', 'florida']. When we loop over each state - does each state is broken like that ['a', 'l', 'a', 'b', 'a', 'm', 'a'] ? and then checked against vowels - ['a','e','i','o','u'] ? I am trying to understand what is happening behind the scenes in here.

Josh Keenan
Josh Keenan
19,652 Points

Here?

state_names = ['Alabama', 'California', 'Oklahoma', 'Florida']
vowels = list('aeiou')

in the loop

Josh Keenan
Josh Keenan
19,652 Points

with that bit or the actual loop?

for state in state_names: 
  state_list = list(state.lower())       # <-- this line?

the actual look

I think I understand the decapitalization part (state_list = list(state.lower())); list looks like that state_list = ['alabama', 'california', 'oklahoma', 'florida'].

When we loop over each state - does each state is broken like that ['a', 'l', 'a', 'b', 'a', 'm', 'a'] ? and then checked against vowels - ['a','e','i','o','u'] ?

I am trying to understand what is happening behind the scenes in here.

Great, thanks Josh.