954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Recursive function

search(l, key):

if len(l) == 0:
return False
elif l[0] == key:
return True
else:
return search(l[1:], key)

I was just wondering if there is any way that i can get rid of "elif" statement. The function is suppose to return False if the key is not in the list "l" and True if otherwise. (it has to be a recursive function though)

And plus...
How would I modify this function so that it returns the index of key in l instead of True? (it still returns False if key is not in l)


I am not very good with computer programming...please someone help me? It would be greatly appreciated. =)
Thanks!

cmpt
Newbie Poster
1 post since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Hi!

The first one without any if-elif:

def search(l, key):
    return key in l

For the second one,.index() is your friend ;)

def search(l, key):
    if key in l: return l.index(key)
    return False


EDIT: Oh sorry, I didn't see it had to be recursive.

def search(l, key, index=0):
    if l:
        if l[0] == key: return index
        return search(l[1:], key, index+1)
    return False


Regards, mawe

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

Is there anyway you can do the last code without adding the index=0 to the search function arguments?

docaholic
Newbie Poster
12 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

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
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

dumb question but...what does if l: do?

docaholic
Newbie Poster
12 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

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
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

i need a recursive function to break a directory so that i can display the elements of the directory in the table.

l = {'key1':['1','2','3'],'key2':['4','5','6']}
return l.values()

i want to show ths directory to be seen in table

and i have to call this code in ZPT.
plz help

Editor's Note:
Please start you own properly titled thread with this question. Don't hijack a solved thread since most people will not look there.

ptirthgirikar
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You