tagged [loops]

How can I avoid "RuntimeError: dictionary changed size during iteration" error?

How can I avoid "RuntimeError: dictionary changed size during iteration" error? I have a dictionary of lists in which some of the values are empty: At the end of creating these lists, I want to remove...

2 Feb at 16:23

Changing objects value in foreach loop?

Changing objects value in foreach loop? In one place i am using the list of string in that case the i am able to change the value of the string as code given below, But for object of class i am not ab...

30 Dec at 12:54

Iterate a certain number of times without storing the iteration number anywhere

Iterate a certain number of times without storing the iteration number anywhere I was wondering if it is possible to perform a certain number of operations without storing the loop iteration number an...

27 Jul at 20:42

Nested FOR loops: readability & performance

Nested FOR loops: readability & performance I understand nested FOR loops. I understand what they do, and how they do it. But my problem is that they seem horribly unreadable to me. Take this example:...

11 Jul at 12:21

C# loops: iterating through an array

C# loops: iterating through an array If I have a loop such as below: does the anonymous string array get created once initially, or recreated once for each pass? I believe the former, but collegues ar...

27 Feb at 14:21

Memory allocation when using foreach loops in C#

Memory allocation when using foreach loops in C# I know the basics on how foreach loops work in C# ([How do foreach loops work in C#](https://stackoverflow.com/questions/398982/how-do-foreach-loops-wo...

23 May at 12:32

How to get property name and its value?

How to get property name and its value? > [C# How can I get the value of a string property via Reflection?](https://stackoverflow.com/questions/987982/c-sharp-how-can-i-get-the-value-of-a-string-prop...

23 May at 12:32

Iterate over C# dictionary's keys with index?

Iterate over C# dictionary's keys with index? How do I iterate over a Dictionary's keys while maintaining the index of the key. What I've done is merge a `foreach`-loop with a local variable `i` which...

2 May at 15:13

Declaring variables inside loops, good practice or bad practice?

Declaring variables inside loops, good practice or bad practice? Is declaring a variable inside a loop a good practice or bad practice? I've read the other threads about whether or not there is a perf...

9 Jan at 22:19

C#: Nested conditionals vs continue statement

C#: Nested conditionals vs continue statement In using ReSharper recently, it is suggesting I reduce nesting in certain places by inverting `if` conditions and using the `continue` statements. ``` for...

26 Jul at 19:13

How to split a long array into smaller arrays, with JavaScript

How to split a long array into smaller arrays, with JavaScript I have an array of e-mails (it can be just 1 email, or 100 emails), and I need to send the array with an ajax request (that I know how to...

25 Oct at 23:18

Is recursion ever faster than looping?

Is recursion ever faster than looping? I know that recursion is sometimes a lot cleaner than looping, and I'm not asking anything about when I should use recursion over iteration, I know there are lot...

25 Oct at 00:32

Identifying last loop when using for each

Identifying last loop when using for each I want to do something different with the last loop iteration when performing 'foreach' on an object. I'm using Ruby but the same goes for C#, Java etc. The o...

1 Jul at 14:0

Object does not support item assignment error

Object does not support item assignment error In my `views.py` I assign values before saving the form. I used to do it the following way: Now, since the list of variables got a bit long, I wanted to l...

23 May at 11:54

Problems removing elements from a list when iterating through the list

Problems removing elements from a list when iterating through the list I have a loop that iterates through elements in a list. I am required to remove elements from this list within the loop based on ...

23 Aug at 08:20

Iterating over ResultSet and adding its value in an ArrayList

Iterating over ResultSet and adding its value in an ArrayList I am iterating over an `ResultSet` and trying to copy its values in an `ArrayList`. The problem is that its traversing only once. But usin...

20 Mar at 09:26

Loop through columns and add string lengths as new columns

Loop through columns and add string lengths as new columns I have a data frame with a number of columns, and would like to output a separate column for each with the length of each row in it. I am try...

24 Jul at 11:39

Stop vs Break in Parallel.For

Stop vs Break in Parallel.For I have difficulty to understand `loopState.Stop()` and `loopState.Break()`. I have read MSDN and several posts about it but I am still confused. What I understand is that...

19 Aug at 13:49

PHP foreach loop through multidimensional array

PHP foreach loop through multidimensional array I have an array: ``` $arr_nav = array( array( "id" => "apple", "url" => "apple.html", "name" => "My Apple" ), array( "id" => "orange",...

Python : List of dict, if exists increment a dict value, if not append a new dict

Python : List of dict, if exists increment a dict value, if not append a new dict I would like do something like that. ``` list_of_urls = ['http://www.google.fr/', 'http://www.google.fr/', 'ht...

12 Feb at 15:38

C# foreach loop with key value

C# foreach loop with key value In PHP I can use a foreach loop such that I have access to both the key and value for example: I have the following code: ``` Regex regex = new Regex(pattern); MatchColl...

28 Feb at 10:19

Batch script loop

Batch script loop I need to execute a command 100-200 times, and so far my research indicates that I would either have to copy/paste 100 copies of this command, OR use a `for` loop, but the `for` loop...

13 Jan at 09:43

How to tell a lambda function to capture a copy instead of a reference in C#?

How to tell a lambda function to capture a copy instead of a reference in C#? I've been learning C#, and I'm trying to understand lambdas. In this sample below, it prints out 10 ten times. ``` class P...

16 Jan at 20:8

Is there a "do ... while" loop in Ruby?

Is there a "do ... while" loop in Ruby? I'm using this code to let the user enter in names while the program stores them in an array until they enter an empty string (they must press enter after each ...

25 Sep at 23:15

Using `continue` keywoard in a switch nest inside a foreach loop

Using `continue` keywoard in a switch nest inside a foreach loop I have the code below (which actually is much longer than you see!) ``` foreach (SensorPair sensor in _sensorPairs) { sensorByte = (b...