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

Shiming Zhang
Shiming Zhang
829 Points

How can I just print the elements begin with an "A"?

I know that I need to use indexing, but I still don't know how to do it.

continents.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]

As = continents

del As[1]
del As[1]
del As[2]
for A in As:
    print("*", A)

2 Answers

Cameron S
Cameron S
20,147 Points

You can do so like this:

continents = [
    "Asia",
    "South America",
    "North America",
    "Africa",
    "Europe",
    "Antarctica",
    "Australia",
]

for obj in continents:
    if obj[0] == "A":
        print(obj)

When you are looping through the list of continents, obj[0] is the first letter (index) of each continent.

Steven Parker
Steven Parker
221,293 Points

Don't modify the list itself! Instead, go back to the code you had from task 1 with the loop that printed out all the items. Then add a conditional ("if") statement to control the "print".

In the conditional expression you can compare the first letter of the name (using indexing) to see if it is equal to "A".

Shiming Zhang
Shiming Zhang
829 Points

I know what you mean, but unfortunately, I still don't what to do. : ( Someone else has helped me with this problem. Thanks a lot. : )

Steven Parker
Steven Parker
221,293 Points

I was trying to help you solve it yourself rather than just doing the solution for you. :see_no_evil: