hope someone can help me with this one. i'm writing round robin algorithm for tournament app. when num of players is odd, i add 'DELETE' in list of players, but latter, when i want to delete all items from schedule list that contain 'DELETE', i cant. one always left. please take look on code, maybe my explanation is confusing, but problem is simple and i suppose it's about lists, just i cant see it :( code: http://codepad.org/IZd0lZl2
problem solved! items from list cant be deleted during iteration as usually. good ppl from stackoverflow gave me this solution: schedule[:] = [x for x in schedule if 'DELETE' not in x]
Your people at stackoverflow were right. When you do a remove on the list, it mutates, and that means iterative actions get an undefined result on an unordered set. You could probably get this to work by doing it backwards, but again, list order is undefined and that method could easily break in a future version of Python. I recommend your solution.
i think that in list like: [1, Delete, 3, Delete, 5...] first method works, but not in case like [1, Delete, Delete, 4...]. index of first 'Delete' is 1, when you remove it index of next 'Delete' becomes 1, and in iteration it will not be removed, after removing first 'Delete' iter will jump to index 2, which is 4 now.
Join our real-time social learning platform and learn together with your friends!