0

Should I create a variable inside a loop?

In the following code I have a list and two loops. In loop 1 I create a variable inside the loop to save time complexity In loop 2 I don't use a variable. The question is makes it sense to do that like loop 1 or should I use loop 2? https://code.sololearn.com/c2h1gljj5RUc/?ref=app

11th May 2022, 1:40 AM
Stefanoo
Stefanoo - avatar
8 Answers
+ 5
I doubt that there is a significant time difference unless you use the variable a lot, say several thousand times. So I would base the decision on readability and coding efficency. Personally I think using a variable is more readable and is easier/faster to type, but you may have other preferences.
11th May 2022, 3:01 AM
Simon Sauter
Simon Sauter - avatar
+ 5
Why not just use for item in list_: ...
11th May 2022, 2:22 AM
Simon Sauter
Simon Sauter - avatar
+ 4
Simon Sauter In any programming language a variable should NOT be created inside a loop. Consider this script in C++ : int _ac = 0; for (int i=0; i<2;i++){ _ac Ă·=1; } This is the correct technique. First the variable is initialized, then Incremented. This way only one address on the call stack is utilized. Else allocation and initialization will be iterative, and hence, redundant. One more caveat, in the latter case, static variables will have to be used. because instance variiables get initialized at each iteration, as mentioned earlier.
13th May 2022, 1:05 AM
Sanjay Kamath
Sanjay Kamath - avatar
13th May 2022, 1:14 AM
Simon Sauter
Simon Sauter - avatar
+ 3
Simon Sauter AFAIK programming languages would be designed to work similarly. Have you heard of shadow loops? ie using the same variable in nested loops (same name). This fails everywhere at run-time, even though most programming environments do not identify them as such. eg)C++
14th May 2022, 11:48 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
How are you saving time complexity in the first loop? Would you not also be using additional memory for the variable creation? /genuine-question But I will say that either works depending on what your overall program needs. There is also the fact that defining the variable just outside the loops works better than your two examples! :)
11th May 2022, 1:58 AM
Justice
Justice - avatar
+ 1
Justice i thought if I write list_[i] the program search for i in the list so if I do that only once per loop I save time. That was the idea. 😅 I know to create a variable cost also time and memory. Define the variable outside the loop would be change the result. Because i changes the value. Simon Sauter i know this kind of loop thanks but in the real code I need i. This was just an example.
11th May 2022, 2:56 AM
Stefanoo
Stefanoo - avatar
0
list_[i] has a time complexity of O(1). If I write var_a = list_[0] # the value is 1 the value 1 becomes a additional label var_a so I now have access to the value 1 with two labels var_a and list_[0] So if I would write item = None for.... : item = list_[i] i create a label for None an then I change the label to the current list item. im didn't know how the labels/names itself will be handled in pythonđŸ€”
13th May 2022, 1:48 PM
Stefanoo
Stefanoo - avatar