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
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
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
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.
Rahul K P
11.9k3 gold badges34 silver badges50 bronze badges
answered Feb 22, 2017 at 10:04
2