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

how do I only print names of continents that start with the letter A?

I can print the list of continents after an asterisk, but I'm unsure how to filter it to only display the specific continent withing the list.

continents.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
# Your code here]
continent = continents[0,3,5,6]
for continent in continents:
    print("* " + continent)

1 Answer

Eric Ryan
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Eric Ryan
Python Development Techdegree Graduate 26,123 Points

It looks like you are trying to manually select the continents that start with “A” and then iterate over every item in the new list and print each item. The way that you are trying to access the indices of the items in the “continents” list won’t work how you would expect as it’s written. The current code also won’t work because you are iterating over the variable “continents”, not the “continent” list that you are trying to create. Besides those issues though, what if the order of the “continents” list were to change? Then the “continent” list would likely be inaccurate.

What you should do is iterate over every item in the “continents” list and check to see if it starts with ‘A’. So you would write the code to say: For every continent in the list, if the first letter in the name of the continent (For example, continent[0]) is equal to ‘A’, print an asterisk and then the name of the continent. If it’s not equal then continue iterating.