Recursion in python 3

Asked

Viewed 37 times

0

def dobra(lista, l_dobro = []):
    if lista == []:
        return 
    else:
        dobro = lista[0] * 2
        l_dobro.append(dobro)
        dobra(lista[1:], l_dobro)
        lista = l_dobro
        return lista

I’m trying to do this function using recursion, and if I call it more than once, it replicates the list that was passed as parameter at the previous time, the l_double in the case. How can I clear her for the next instance?

  • try to use l_dobro.clear()

  • Hello Bruno, welcome back! It is easier to help you if you tell us where/how you want to use this "fold" function, maybe there are other easier ways to achieve the same result. ;-)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.