Recursive function in Python does not change list

Abdalah El-Barrad

I am using a recursive function on a list of lists with an accumulator, but instead of creating the right list of lists, it makes a list with duplicates of the last item inserted in the accumulator. I recreated the problem in a much simpler recursive function and list. This function takes a list of lists and makes 2 copies and reverses them n times.

def recursauto(x, n, instSet):
    #Base Case
    if(n==0):
        instSet.append(x)
        print(x) #Print to see what SHOULD be added
    else:
        temp = [x]*(2)  # Make a list with 2 copies of the original list
        for i in range(len(temp)):
            temp[i][i] = temp[i][i][::-1] # Make the list backwards
        for i in range(2):
            recursauto(temp[i], n-1, instSet) #Input each element

MyList = [] #Empyt list for accumulator
print("Correct output:")
recursauto([["A", "l", "e", "x"], ["A", "b", "d", "a", "l", "a", "h"]], 2, MyList)

print("Wrong printed list:")
for i in MyList:
    print(i) #Print what is in the accumulator

The output comes out wrong and the accumulator does not have the right things that were put into it.

Correct output:
[['A', 'l', 'e', 'x'], ['A', 'b', 'd', 'a', 'l', 'a', 'h']]
[['A', 'l', 'e', 'x'], ['A', 'b', 'd', 'a', 'l', 'a', 'h']]
[['x', 'e', 'l', 'A'], ['h', 'a', 'l', 'a', 'd', 'b', 'A']]
[['x', 'e', 'l', 'A'], ['h', 'a', 'l', 'a', 'd', 'b', 'A']]
Wrong printed list:
[['x', 'e', 'l', 'A'], ['h', 'a', 'l', 'a', 'd', 'b', 'A']]
[['x', 'e', 'l', 'A'], ['h', 'a', 'l', 'a', 'd', 'b', 'A']]
[['x', 'e', 'l', 'A'], ['h', 'a', 'l', 'a', 'd', 'b', 'A']]
[['x', 'e', 'l', 'A'], ['h', 'a', 'l', 'a', 'd', 'b', 'A']]

I know that there is an easier way to do this, but the function I'm actually making requires recursion. Like I said, this is just a simplified recreation of the problem I am having.

obataku
temp = [x]*(2)

The above line does not create a list of two copies of the original list; in fact, it just stores a reference to the same original list twice. If you want a distinct copy of x, try using the list copy constructor like temp = [list(x), list(x)] or alternatively the shallow copy method [x.copy() x.copy()].

See the below example.

>>> ls = ['a']
>>> dup = [ls] * 2
>>> dup
[['a'], ['a']]
>>> ls.append('b')
>>> dup
[['a', 'b'], ['a', 'b']]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursive sorting function for list in Python

From Dev

Python recursive list search function

From Dev

Python recursive list search function

From Dev

How to change this recursive function (return a list of paths for a 3X3 matrix) into iterative function in Python 2.7?

From Dev

recursive max function for list of list python

From Dev

Does Python allow a recursive __iter__ function?

From Dev

Why does this recursive python function return None?

From Dev

Recursive Python function to produce a list of anagrams

From Dev

Recursive Python function to count occurrences of an element in a list

From Dev

Python, not able to append to a list from a recursive function

From Dev

Modifying a list outside of recursive function in Python

From Dev

How to return a list from a recursive function in Python?

From Java

Recursive function for list of lists

From Dev

Recursive function of a list

From Dev

Recursive Function on a List in Haskell

From Dev

Recursive Files list function

From Dev

empty list on recursive function

From Dev

Recursive function in Python: Best way to get a list of specific nested items

From Dev

python - return flat list of objects from recursive function

From Dev

Return a flatten list from my recursive function in Python

From Dev

Why this recursive backtracking function slower than the non-recursive one for calculating minimum number of coins for change in python?

From Dev

python recursive list questions

From Dev

Python list recursive changes

From Dev

python recursive function calls

From Dev

Writing a recursive function in Python

From Dev

Python recursive Factorial Function

From Dev

Python writing recursive function

From Dev

Python not defined recursive function?

From Dev

python generator in recursive function

Related Related

  1. 1

    Recursive sorting function for list in Python

  2. 2

    Python recursive list search function

  3. 3

    Python recursive list search function

  4. 4

    How to change this recursive function (return a list of paths for a 3X3 matrix) into iterative function in Python 2.7?

  5. 5

    recursive max function for list of list python

  6. 6

    Does Python allow a recursive __iter__ function?

  7. 7

    Why does this recursive python function return None?

  8. 8

    Recursive Python function to produce a list of anagrams

  9. 9

    Recursive Python function to count occurrences of an element in a list

  10. 10

    Python, not able to append to a list from a recursive function

  11. 11

    Modifying a list outside of recursive function in Python

  12. 12

    How to return a list from a recursive function in Python?

  13. 13

    Recursive function for list of lists

  14. 14

    Recursive function of a list

  15. 15

    Recursive Function on a List in Haskell

  16. 16

    Recursive Files list function

  17. 17

    empty list on recursive function

  18. 18

    Recursive function in Python: Best way to get a list of specific nested items

  19. 19

    python - return flat list of objects from recursive function

  20. 20

    Return a flatten list from my recursive function in Python

  21. 21

    Why this recursive backtracking function slower than the non-recursive one for calculating minimum number of coins for change in python?

  22. 22

    python recursive list questions

  23. 23

    Python list recursive changes

  24. 24

    python recursive function calls

  25. 25

    Writing a recursive function in Python

  26. 26

    Python recursive Factorial Function

  27. 27

    Python writing recursive function

  28. 28

    Python not defined recursive function?

  29. 29

    python generator in recursive function

HotTag

Archive