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 trialShachar Bobrovskye
1,708 Pointswho can i check the location of item
How can i check the value of item and skip it?
def loopy(items):
for item in items:
if item == 'a' :
continue
print(item)
3 Answers
Travis Bailey
13,675 PointsYou actually did that in your code.
if item == 'a':
continue
That code looks at the current item from items that you're looping through. It checks to see if the item is 'a' and if it is it continues the loop without doing anything, effectively skipping it.
If you're trying to check the value of a specific character of the item you'd need to provide a slice.
if item[2] == 'a':
continue
The above instead of checking if the entire item is equal to 'a', instead looks at the 3rd character in the item. If the 3rd character is 'a' it skips it just like in the previous example. Keep in mind when you're providing a slice or typically referring to any position in an iterable the first position or first item is at the 0 index, not 1. So when checking the 3rd character I specify a 2 instead of 3.
So for example if the item was 'apple' my code wouldn't skip it since the third letter is a 'p', but it it was 'aaapple' it would skip it since the 3rd character is an 'a'.
Shawn Denham
Python Development Techdegree Student 17,801 PointsAssuming that list you are looping though has that information (value or location) you can just add additional conditions in your if statement.
Shachar Bobrovskye
1,708 PointsMany Thanks, It work for me..