I have built this program to take any number of lists of any size, and output a new list nlist where all individuals in the list have been sorted one after another in order from the first list to last. An example would be to input the lists [1,2,3,4] and [5,6,7,8] and output [1,5,2,6,3,7,4,8] creating a sort of shuffled type thing.
My reasoning for this code is that all lists to be shuffled would be individuals of a larger container list. The program starts with an if statement checking whether or not the container list contains anything. Then it runs through the x of the container list which are the lists to be shuffled. In this loop, it checks to see whether or not the list x contains any individuals and to remove that list if it does not. After that, it would add the first number of x to the new list and remove it from x. After it has done this it will recur so that it can do it again with the new x[0] until all of the lists are empty and all x[0] shuffled into the new list.
The problem is, when I run this it comes up with a list index out of range error. I assume this is because at the end of the program a number of x's end up being empty but the program registers the container list as being full because it contains these empty lists. It then removes them from the list and tries to run the rest of the program but can't because there is nothing to run it on. I believe this because it does end up printing out the shuffled list but still comes up with an error. I tried fixing this by adding a recursion after the list.remove(x) so that it could run the program again with the removed x.
Any ideas on how to solve this problem?
def shuffle(list, nlist):            #list is a list of lists to be shuffled
    if list:                         #checks for a completed task
        for x in list:               #runs through lists to be completed
            if not x:                #checks if a list is empty
                list.remove(x)       #if empty removes that list
                shuffle(list, nlist) #recurs the function
            nlist.append(x[0])       #adds 0 index of x to nlist
            x.remove(x[0])           #removes 0 index of x from x
        shuffle(list, nlist)         #recurs the function until task is complete
    else:
        print(nlist)                 #prints end result`enter code here`
 
     
    