Python recursion append to list

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

MSeifert

138k32 gold badges318 silver badges333 bronze badges

asked Feb 22, 2017 at 9:54

Python recursion append to list

4

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 Feb 22, 2017 at 10:06

MSeifertMSeifert

138k32 gold badges318 silver badges333 bronze badges

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

Adirio

4,8801 gold badge14 silver badges25 bronze badges

answered Feb 22, 2017 at 10:00

Python recursion append to list

Moses KoledoyeMoses Koledoye

75.8k8 gold badges124 silver badges133 bronze badges

2

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

Adirio

4,8801 gold badge14 silver badges25 bronze badges

answered Feb 22, 2017 at 9:58

3

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 Feb 22, 2017 at 9:58

Python recursion append to list

Abhishek JAbhishek J

2,2662 gold badges21 silver badges21 bronze badges

1

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.

Python recursion append to list

Rahul K P

11.9k3 gold badges34 silver badges50 bronze badges

answered Feb 22, 2017 at 10:04

Python recursion append to list

2

How do you add elements to a recursion list in Python?

sum number in a list python using recursion.
def listsum(numList):.
if len(numList) == 1:.
return numList[0].
return numList[0] + listsum(numList[1:]).
print(listsum([1,3,5,7,9])).

How do you implement a recursive function in python?

Python also accepts function recursion, which means a defined function can call itself. Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.

What is recursion example?

Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself. For example, we can define the operation "find your way home" as: If you are at home, stop moving. Take one step toward home.