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 trial

Python Python Basics (2015) Shopping List App Continue

David Willis
seal-mask
.a{fill-rule:evenodd;}techdegree
David Willis
Full Stack JavaScript Techdegree Student 18,600 Points

Why can't I use item.index('a') == 0 instead of item[0] == 'a'

Why doesn't the following work... I get an error that says "Substring not found", but shouldn't the code work essentially the same way?

def loopy(items): for item in items: if item.index('a') == 0: continue else: print(item)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

According to the docs:

list.index(x)
Return the index in the list of the first item whose value is x. It is an error if there is no such item.

So if there is no 'a', an error is raised.

See: (https://teamtreehouse.com/community/shopping-list-4) for help on the same challenge