How to modify or delete items from an enumerable collection while iterating through it in C#

Iterating Backwards through the List sounds like a better approach, because if you remove an element and other elements “fall into the gap”, that does not matter because you have already looked at those. Also, you do not have to worry about your counter variable becoming larger than the .Count. List<int> test = new List<int>(); … Read more

Set iteration order varies from run to run

The reason the set iteration order changes from run-to-run appears to be because Python uses hash seed randomization by default. (See command option -R.) Thus set iteration is not only arbitrary (because of hashing), but also non-deterministic (because of the random seed). You can override the random seed with a fixed value by setting the … Read more

Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin

There’s nothing wrong with direct, “ad-hoc” solution. It can be clean enough too. Just notice that spiral is built from segments. And you can get next segment from current one rotating it by 90 degrees. And each two rotations, length of segment grows by 1. edit Illustration, those segments numbered … 11 10 7 7 … Read more

Iteration over list slices

If you want to divide a list into slices you can use this trick: list_of_slices = zip(*(iter(the_list),) * slice_size) For example >>> zip(*(iter(range(10)),) * 3) [(0, 1, 2), (3, 4, 5), (6, 7, 8)] If the number of items is not dividable by the slice size and you want to pad the list with None … Read more