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 trialDharmesh Nayee
1,090 PointsIm Stuck on challenge Task 2 of 2...
I need to list every continent that begins with A but i am not sure how to do this.
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent in continents:
print("* " + continent)
1 Answer
Hakim Rachidi
38,490 PointsHi Dharmesh,
To achive this you have multiple options
Use if
for continent in continents:
if continent[0] == "A":
print("* " + continent)
As you can index strings continent[0]
is the first character of continent.
Use filter()
for continent in filter(lambda c: c[0] == "A", continents):
print("* " + continent)
Both work fine, I tend to use the if
in the loop (with filter()
you are first filtering for all continents that start with 'a' and then loop over them), as it looks cleaner and is more performant;
Dharmesh Nayee
1,090 PointsDharmesh Nayee
1,090 PointsThank you Hakim.
Can you explain if my understanding is correct..
The if function is looking at the first letter in the variable continent to check if it is equal to A.
Hakim Rachidi
38,490 PointsHakim Rachidi
38,490 PointsCorrect, but there is no 'if function' if is a construct, a construct is on the language level (build into the interpreter), a function is on the higher library level (code you write or from standard import).