list loop and drop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: list loop and drop (/thread-24558.html) |
list loop and drop - 3Pinter - Feb-19-2020 Hi, I have a list and I loop it: grab first item and do something with each item in the list after that is done: no more need for first item. grab second item and do something with each remaining item in the list after that is done: no more need for second item. etc To visualize it: [1,2,3,4,5] 1 will do something with 2,3,4,5 .... and drops 1 now the list is 2,3,4,5 2 will do something with 3,4,5 .... and drops 2 now the list is 3,4,5 itertool combinations makes a new list with all the unique combinations. Which kinda is what I'm doing HOWEVER given a list of thousands+ I THINK it's faster to yield the original list and somehow drop used item. Makes sense? Curious how to do this. RE: list loop and drop - michael1789 - Feb-19-2020 I imagine someone else can do it in one line, but here's what I thought up, and it works. list_of_items = [1, 2, 3, 4, 5] work_list = list_of_items.copy() del work_list[0] for item in list_of_items: try: print(sum(work_list)) del work_list[0] except IndexError: print("Done") |