Python recursion in appending lists

Question:

I want to append to a list recursively but I cannot come up with a function that works. The function takes two arguments times and data. times should be the number of times to append the data.

Here is my code so far:

def replicate_recur(times, data):
    result2 = []
    if times == 0:
        result2.append(data)
    else:
        result2.append(data)
        replicate_recur(times - 1, data)
    return result2
Asked By: Nix

||

Answers:

Because your redefining result2 everytime. Keep result2 outside the function and it should work.

Also you could consider doing data*times to replicate if data is a list or simply do

(result2.append(data))*times
Answered By: Abhishek J

You can use xrange for this, there is no point to use recursion unless it is a coding test.

def replicate(times, data):
    result2 = []
    for i in xrange(times):
        result2.append(data)
    return result2

Same function can be written in a recursive way like this:

def replicate_recur(times, data, listTest=None):
    # If a list has not been passed as argument create an empty one
    if(listTest == None):
        listTest = []
    # Return the list if we need to replicate 0 more times
    if times == 0:
        return listTest
    # If we reach here at least we have to replicate once
    listTest.append(data)
    # Recursive call to replicate more times, if needed and return the result
    replicate_recur(times-1, data, listTest)
    return listTest
Answered By: pushpendra chauhan

To make your code work, you need to extend the list in the current execution with the output of the next recursive call. Also, the lowest depth of the recursion should be defined by times = 1:

def replicate_recur(times, data):
    result2 = []
    if times == 1:
        result2.append(data)
    else:
        result2.append(data)
        result2.extend(replicate_recur(times - 1, data))
    return result2

On another note, you can simply replicate your list with:

def replicate(times, data):
    return [data]*times
Answered By: Moses Koledoye

In the recursion, each time replicate_recur is called, a fresh result2 in new name space is created.

[data] * times

Would do what you are trying to achieve.

You could use a intermediate list to append to in each recursive call. That avoids these redefinition problems you’re encountering currently:

def replicate_recur(times, data, result=None):
    if result is None:  # create a new result if no intermediate was given
        result = []
    if times == 1:
        result.append(data)
    else:
        result.append(data)
        replicate_recur(times - 1, data, result)  # also pass in the "result"
    return result

When called:

>>> replicate_recur(4, 2)
[2, 2, 2, 2]
Answered By: MSeifert

Python uses ‘Pass-by-Object-reference’, which is why either of the below code should do the trick in your case.

def replicate_recur(times, data, result2 = []):
    if times == 1:
        result2.append(data)
    else:
        result2.append(data)
        replicate_recur(times - 1, data)
    return result2

When called:

>>> replicate_recur(4, 2)
[2, 2, 2, 2]

Alternatively, you could create the result2 list and pass it as an argument to the function. The argument is ‘Passed by Object reference’, so the same result object is being modified inside the function as well.

def replicate_recur(times, data):
    if times == 1:
        result2.append(data)
    else:
        result2.append(data)
        replicate_recur(times - 1, data)
    return result2

When called:

>>> result2 = []
>>> replicate_recur(4, 2)
[2, 2, 2, 2]

Refer to the below link to learn more about Pass by Object Reference.
Python:Pass By Object Reference

Answered By: Vijay S
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.