there is a function which does some activities; def new(userlist=[]): for user in userlist: print("I am doing some stuff here for {}".format(user)) # done the required activity on the user and now thinks on doing nested activity. nested_userlist = get_inviters(user) new(nested_userlist) # end of function def get_inviters(user): # code code return [...] What I can't do is, e.g. if I set a nested activity [recursion] level e.g. 5, I don't know how to handle it. It ends up going infinitive. What I want is, e.g. with the nested activity level of 2, it should do this main_users = [5 users here] For every main user in those 5 main users, go and do stuff for them; And then if there is a nested activity available at the current level, do it. If visualized: Main_user1: #some stuff here Nested_level1_user1: #some stuff here Nested_level2_user1: #some stuff here Nested_level2_user2: #some stuff here Nested_level1_user2: #some stuff here .... . . .