Posts: 39 Threads: 14 Joined: Oct 2018 List = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] step = 4 def ResetCR (l, stp): K = [] if stp != 0: for i in range(1, int(len(l)/stp)+1): K.append(l[i*stp - 1]) l = [x for x in l if x not in K] stp = stp - 1 return ResetCR(l,stp) if stp == 0: return K F = ResetCR(List,step) print (F) I'm trying to change the order of a list. If the "K = []" is out side the function, it works. However, I'm wondering is there a way to keep the variable 'K' inside the function? Posts: 4,216 Threads: 97 Joined: Sep 2016 Is there some reason you need to use recursion? def reset2 (l, stp): k = [] for s in range(stp, 0, -1): for i in range(1, int(len(l) / s) + 1): k.append(l[i * s - 1]) l = [x for x in l if x not in k] return k Posts: 39 Threads: 14 Joined: Oct 2018 (Mar-05-2019, 03:34 PM)ichabod801 Wrote: Is there some reason you need to use recursion? def reset2 (l, stp): k = [] for s in range(stp, 0, -1): for i in range(1, int(len(l) / s) + 1): k.append(l[i * s - 1]) l = [x for x in l if x not in k] return k Just to practice recursion... Thank you for the solution! Posts: 4,216 Threads: 97 Joined: Sep 2016 In terms of recursion, my first thought was to have K be a parameter, defaulting to an empty list, and passing the modified K when you recurse. However, there are problems with using empty lists as parameter defaults. You would have to use None as the default, and then replace it with a list in the body of the function (if it is None). Posts: 39 Threads: 14 Joined: Oct 2018 (Mar-05-2019, 04:11 PM)ichabod801 Wrote: In terms of recursion, my first thought was to have K be a parameter, defaulting to an empty list, and passing the modified K when you recurse. However, there are problems with using empty lists as parameter defaults. You would have to use None as the default, and then replace it with a list in the body of the function (if it is None). My question is if I replace K in the function with a list and the list is not empty, how does the ".append" effect the list or should I still use ".append" to change the list? Also, in that way, I need a K outside the function. Posts: 4,216 Threads: 97 Joined: Sep 2016 I'm not sure I understand the question. If you have a K in the function as a non-empty list, append will add to what is already in the list. But if you are defining K in the function, you will have the same problem of it resetting every time you recurse. But if you have a default, and pass the modified list each time you recurse, it doesn't reset. Posts: 39 Threads: 14 Joined: Oct 2018 (Mar-05-2019, 06:32 PM)ichabod801 Wrote: I'm not sure I understand the question. If you have a K in the function as a non-empty list, append will add to what is already in the list. But if you are defining K in the function, you will have the same problem of it resetting every time you recurse. But if you have a default, and pass the modified list each time you recurse, it doesn't reset. Thank you! It is very helpful. I think I figured it out. List = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] step = 4 def ResetCR (l, stp, K = None): if K is None: K = [] if stp != 0: for i in range(1, int(len(l)/stp)+1): K.append(l[i*stp - 1]) l = [x for x in l if x not in K] stp = stp - 1 return ResetCR(l,stp,K) if stp == 0: return K F = ResetCR(List,step) print (F) |