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 Python Basics (2015) Shopping List App Continue

who can i check the location of item

How can i check the value of item and skip it?

breaks.py
def loopy(items):
  for item in items:
    if item == 'a' :
       continue
    print(item)

3 Answers

Travis Bailey
Travis Bailey
13,673 Points

You 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'.

Assuming that list you are looping though has that information (value or location) you can just add additional conditions in your if statement.

Many Thanks, It work for me..