Yes there is
def search(l, key):
if l:
if l[0] == key:
return 0
s = search(l[1:], key)
if s is not False:
return s + 1
return False
However, the good way to do this is to use the builtin index function !
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
It checks to see if l is existant, if there are no elelments in l then it will return False because your key cant be in an empty list! Therefore avoiding any unwanted Exceptions! :)
Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
BTW, the letter 'l' is a rather poor choice for a variable name, since it looks much like the number '1' on many editors. I have gotten in the habit to use 'mylist' or 'qq' for casual lists.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417