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 trialShiming Zhang
829 PointsHow 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 = [
'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
20,537 PointsYou 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
231,269 PointsDon'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
829 PointsI 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
231,269 PointsI was trying to help you solve it yourself rather than just doing the solution for you.
Shiming Zhang
829 PointsShiming Zhang
829 PointsYou're a star!