-2

How do I write a code in python by only using the recursion without loops and any build in methods or functions? I tried:

def myRemove(x, cont): # x is a string inside of the list cont
    if x == cont:
        return None
    elif len(x) > len(cont):
        return None
    else:
        if "x" not in cont[0]:
            return myRemove(x,cont[1:])
        else:
            return cont
6
  • Why do you wanna go for recursion for deleting an item from a list?
    – bumblebee
    May 20, 2019 at 10:21
  • what is x, what is cont? could you provide examples, inputs and expected outputs? minimal reproducible example May 20, 2019 at 10:21
  • what is cont in your function? May 20, 2019 at 10:22
  • 3
    @bumblebee homework obviously May 20, 2019 at 10:22
  • if you want to delete item index 3 from a list called my_list you can simply write my_list.pop(3)
    – user10417531
    May 20, 2019 at 10:23

2 Answers 2

3

Some problems I see in your code:

1. Difference between a string and a variable

You have the following line in your code which is semantically wrong:

if "x" not in cont[0]:
    ...

Here "x" is the string 'x' and not the value of x. To fix this remove the quotation marks.

if x not in cont[0]:
    ...

2. Difference between list and variable

To check if a variable is in a list use in. e.g.

>>> "test" in ["test", "wow", "u"]
true

To check if a variable is equal to another variable use ==. e.g.

>>> "test" == ["test", "wow", "u"][0]
true

The fixed part of your code: (Because cont[0] returns a value and not a list)

if x == cont[0]:
    ...

3. Returns in recursion

You have to concatenate the returned list with the list part before the other list. Otherwise, you are always returning the last part of the list.

One possible solution

def remove(string, string_list):
    if string_list[0] == string:
        return string_list[1:]
    else:
        return string_list[:1] + remove(string,string_list[1:])
0
2
def recursive_remove(x: str, cont: list):
    """ removes items equal to x using recursion only
        cont: list of strings
        x: string to remove from list
    """
    if len(cont) == 0:
        return []
    if cont[0] != x:
        return [cont[0]] + recursive_remove(x=x, cont=cont[1:])
    else:
        return recursive_remove(x=x, cont=cont[1:])


list_without_banana = recursive_remove(x='banana', cont=['apple', 'banana', 'strawberry', 'peanut'])
print(list_without_banana)
>>>['apple', 'strawberry', 'peanut']
3
  • The code you have posted seems to be correct, but it would have been better if it was written to match the apparent api that the OP posted. This would help future readers to match up the two pieces of code.
    – quamrana
    May 20, 2019 at 10:53
  • @quamrana I think the code the OP posted doesn't make any sense, when x is a String and cont a list of stings. He compares a string with a list which is definetly semantically wrong May 20, 2019 at 11:05
  • 1
    @UliSotschok I just meant the order and names of the parameters. I realise that the OP seems to have no idea about what the semantics of the parameters are.
    – quamrana
    May 20, 2019 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.