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 Removing Items From A List

Removing vowels.

Whay is not my code removing the vowls from famous_cities, i keep getting

Traceback (most recent call last): 
  File "Vowels.py", line 8, in <module>   
    new_capitels = item.remove(vowls) 
AttributeError: 'str' object has no attribute 'remove'    :
vowls = ["a", "e", "i", "o", "u"]

famous_cities = ["Muqadishou", "Dubai", "Oslo", "Nairopi"]

for item in famous_cities:
  new_capitels = item.remove(vowls)
  print(new_capitels)

Hard to tell, it could be to do with your indenting. does it run?

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

What you need is a way to scan a famous_city, removing the letters in vowls and print the result as a string:

vowls = ["a", "e", "i", "o", "u"]

famous_cities = ["Muqadishou", "Dubai", "Oslo", "Nairopi"]

for city in famous_cities:
    new_capitol_charlist = []
    # For each character in city
    for char in city:
        # if the lower case version is not in vowls list
        if char.lower() not in vowls:
            # append to new_city_charlist
            new_capitol_charlist.append(char)
    # join new_capitol_charlist into string and print
    print(''.join(new_capitol_list))

# The above can be accomplished in a using a list comprehension
for city in famous_cities:
    new_capitol = ''.join([char for char in item if char.lower() not in vowls])
    print(new_capitol)

yeah it runs and gives me back Traceback (most recent call last):
File "Vowels.py", line 8, in <module>
new_capitels = item.remove(vowls)
AttributeError: 'str' object has no attribute 'remove'

Matthew Rigdon
Matthew Rigdon
8,223 Points

There is no .remove method for strings. You can use .replace to remove a letter from a string. Example: newstr = oldstr.replace("M", ""). This would replace M's with "", or nothing.

http://stackoverflow.com/questions/3559559/how-to-delete-a-character-from-a-string-using-python