Home > OS >  Recursive function in Python returns only the last list value
Recursive function in Python returns only the last list value

Time:11-22

I'm relatively new to programming and completely new to Python. I'm currently practicing recursive functions.

I was trying to implement a simple sorting function that searches recursively for the lowest value of a given list and appends it to a new list.

However, when returning the list, it only returns the last (highest) value of the list. What confuses me the most is that if I put a print(result) right before returning the result, the function prints the entire sorted list.

Here's my code. PS: the function smallesListIndex searches for the index of the lowest value of a given list. Function:

def recSort(liste, result=None):
listIn = liste
length = len(liste)
smallestIndex = smallestListIndex(listIn)

# To prevent creating a new list in every recursion, an intermediate list is to be created.
if result is None:
    result = []
    
if length == 1:
    result.append(listIn[smallestIndex])
    print(result)
    return result
    
else:
    result.append(listIn[smallestIndex])
    listIn.pop(smallestIndex)      
    length -= 1
    # the intermediate list is to be given to the next recursion.
    return recSort(listIn, result)

Main:

if __name__ == "__main__":
     liste = [5, 2, 4, 8, 7, 10, 6]
     recSort(liste)
     print(liste)

Output:

[2, 4, 5, 6, 7, 8, 10] #  Output from the print function
[10] # Output from the main function

Can anyone help me?

CodePudding user response:

Your function recSort return a list, and in your example, you don't use it.

Try, and see result:

if __name__ == "__main__":
    liste = [5, 2, 4, 8, 7, 10, 6]
    res = recSort(liste)
    print(res)

To explain, your function modifies the input list. That's why you only get one value.

If you don't want modification of your input, you can try listIn = liste[:] in your function.

CodePudding user response:

  1. Your list liste is not the same because without using copy function, you're just creating refferences, so whatever you do to the refference, you do it to the original list.
  2. You're returning things from the recSort function, but you're not saving them anywhere. You can use in main something like result = recSort(liste) and then print(result).

CodePudding user response:

If instead of print(liste) you do print(recSort(liste)) then your code works.

Full corrected code below, also added my simple implementation of smallestListIndex() for the purpose of complete runnable example, and reformatted code.

Try it online!

def smallestListIndex(l):
    return l.index(min(l))

def recSort(liste, result=None):
    listIn = liste
    length = len(liste)
    smallestIndex = smallestListIndex(listIn)

    # To prevent creating a new list in every recursion, an intermediate list is to be created.
    if result is None:
        result = []
        
    if length == 1:
        result.append(listIn[smallestIndex])
        #print(result)
        return result
        
    else:
        result.append(listIn[smallestIndex])
        listIn.pop(smallestIndex)      
        length -= 1
        # the intermediate list is to be given to the next recursion.
        return recSort(listIn, result)

if __name__ == "__main__":
     liste = [5, 2, 4, 8, 7, 10, 6]
     print(recSort(liste))

Output:

[2, 4, 5, 6, 7, 8, 10]
  • Related