Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Lists Redux Disemvoweled

Andy Bosco
PLUS
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
'aeiou'
>>> word.remove(vowel)
>>> word
['Treehouse']
>>> word.remove(vowel)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list