Search code examples
pythonloopsnestednested-loops

How to nest loops N number of times python


I'm creating a program, which needs to nest loops N number of times. Is there any way to do this? Or do I need to do it manually?

Wanted result:

for i in range(N):
    #Do something
    for i in range(N-1):
        #Do something
        for i in range(N-2):
            #Etc, continues until the value in range is 0

Solution

  • You can do that with recursion

    def func(N)
        if N == 0: return
        for i in range(N):
            #do something
        return func(N-1)