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!

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

Andy Bosco
Andy Bosco
Courses Plus Student 681 Points

state_list.remove(vowel). "state_list" is a List and "vowel" is A string.

At state_list.remove(vowel). "state_list" is a List and "vowel" is A string. Python allows the remove of the String inside the List. When I tried to test my code below it won't work and got the error ValueError: list.remove(x): x not in list. Why my code doesn't work? My variable "word" is a List and "vowels" is a String.

word = ['Treehouse'] vowels = ('aeiou') word.remove(vowels) print word

2 Answers

Remember, to convert a 'string' to a list you can use vowel_list = list('aeiou')

This will create vowel_list as a list with the 5 elements of ['a', 'e', 'i', 'o', 'u']

You can then iterate over the vowel list

list.remove = remove(...)
    L.remove(value) -> None -- remove first occurrence of value.
    Raises ValueError if the value is not present.

list.remove will remove an item from a list, but it won't filter out parts of a string nested inside the list. By saying ["Treehouse"].remove("aeiou"), you're telling Python to remove the string "aeiou" from the list, not from the string "Treehouse" inside the list.

For example:

>>> word = ['Treehouse', 'aeiou']
>>> vowel = 'aeiou'
>>> word
['Treehouse', 'aeiou']
>>> vowel
>>> word.remove(vowel)
>>> word
>>> word.remove(vowel)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list