[Solved] list manipulation and recursion


It’s somewhat unclear to me if you are intentionally trying to skip items on each pass or if that’s accidental on your part as a side effect of your code. That is, it looks like a bug to me, but maybe it’s a feature and not a bug.

If I wanted to remove 5 items from a list as a group and do something with them then recurse the function, I’d do this:

def fill_layout(images):
    out_items = []
    while len(out_items) <5 and images:
        out_items.append(images.pop(0))
    # do something to your 5 or less items in out_items
    if images:
        fill_layout(images)

Note, you don’t need to recurse, you could just handle everything in the function. Further, you could just slice the list into 5 lengths and handle each one as you go. There is a lot of artificial complexity in your method and I don’t know how much is actually needed from your example — so I did this to keep the groups of 5, remove from list and recurse. There are probably simpler ways to do what you want.

4

solved list manipulation and recursion