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 Introducing Lists Using Lists Continental

Stuck on challenge task asking for list items starting with A

Hi everyone! I am stuck on the challenge item asking for continents list with only continents starting with A. I went back to the video and it is not in there. I then tried to research online but I didn't find the right code. Finally, I tried using remove to take the items out of the list that didn't begin with A (which I know is neither DRY nor correct...but maybe tricky for an attempt?) Anyway, I'm stuck. I would appreciate some help. Thanks!!

continents = [
    'South America',
    'North America',
# Your code here
print("continents: ")
for continent in continents:
    print("* " + continent)
A_list1 = continents.remove("South America")
A_list2 = A_list1.remove("North America")
A_list3 = A_list2.remove("Europe")

2 Answers

The hint given for challenge 2 is HINT: Remember that you can access characters in a string by index. In particular, continent[0] gives the first letter of the continent.

Try using an if statement to compare the first letter to "A" before printing the continent name.

A_list1 = continents.remove("South America") removes "South America" from continents, and sets A_list1 to None. A_list2 = A_list1.remove("North America") attempts to call remove() on None, which gives an AttributeError: 'NoneType' object has no attribute 'remove'.

To modify the continents array,

continents.remove("South America")
continents.remove("North America")

This does not pass the challenge, since the continents array isn't meant to be modified and the array isn't printed in the format specified in the first part of the challenge. You could instead create a copy of the continents array and remove elements from the copy without modifying the original continents array.

continentsA = continents[:]
continentsA.remove("South America")
continentsA.remove("North America")

for continent in continentsA:
    print("* " + continent)

Thank you very much for offering me support. This is my very first course in Python, and I wasn't really having any difficulties until now. I saw the hint, but I honestly don't have a clue what access characters by index means. I have watched all the videos and completed all the tests and exercises, but I am really not understanding this one. Thanks again for your help.