You can start a loop at whatever you want. The reason you see loops starting at zero often, is because they are looping through an array. Since the first item in an array is at index '0', it makes sense to start looping from 0 to access every item in an array.
Answer from Aksel on Stack ExchangeI am pulling data from a dictionary and appending it to a list, but it appears that dictionary keys start at 1, not 0.
We have been taught to use for loops for this kind of task, but they initialize at 0. Can I start at 1? If not, what is an alternative? Thanks!
for i in range(len(dict)):
<code goes here>Videos
You can start a loop at whatever you want. The reason you see loops starting at zero often, is because they are looping through an array. Since the first item in an array is at index '0', it makes sense to start looping from 0 to access every item in an array.
You should use i = 0 in situations where iterating starting at zero would be natural, as previously stated this could include array indexing or similar. Personally I would say I use this style at least 90% of the time, as when modelling problems in the computer we mold them to use built in data structures, which usually start at 0. Our minds become use to working in this style.
Starting at i = 1 is more natural for modeling many problems while designing algorithms. For example, if you are given a problem such as person 1 is x years old, person 2 is y years old, and so on, indexing using the given numbers may make it easier to give an answer if asked something such as who is the youngest person in the list. Experience and experimentation will teach you if this is worthwhile, in my experience, this can be helpful in things such as programming competitions (like the ICPC), where algorithms must be developed quickly with not much time for debugging, and the algorithms can be very complex, so the clarity is important.
In other words it may be beneficial to waste this first index if it adds clarity, however experienced programmers quickly learn to understand both styles.
However, if you start at zero, remember to be aware that the arrays still start at zero, and you will need an n + 1 size array to represent n elements if you use indexing starting at 1.
Also be aware of your conditional in the for loop.
for (int i = 0; i < 10; i++) - Will loop 10 times
for (int i = 1; i <= 10; i++) - Will loop 10 times
It is basic, but a common source of errors that may be hard to track down, especially for beginning programmers.
The for index in range(len(list)) loop executes the loop body with index first set to 0, then 1, then 2, etc. up to len(list) - 1. The previous value of index is ignored and overwritten. If you want index to start at iteration + 1, use the 2-argument form of range:
for index in range(iteration + 1, len(list)):
You really should be using enumerate for stuff like this, as you can loop through the index and the value at the same time (which will save you the hassle of using two for-loops).
for i, j in enumerate(list):
print i, j
Your inner loop is overriding the variable index that you defined in the first loop.
l = [a, b, c, d]
for x in l
print(x)Can this start at an index other than 0?
You can avoid index errors with sum(v[i+1:i+4]). You have to compare each element (if v[i] == 0) not the whole list.
v = [0, 5, 5, 0, 6, 6, 0, 7, 7 ]
for i in range(len(v) - 1, -1, -1):
if v[i] == 0 :
v[i] = sum(v[i+1:i+4])
print(v)
You can use itertools.accumulate on reversed list to get the sums and then switch to the sum in positions of where 0s exist in the original -
from itertools import accumulate
acc = list(accumulate(reversed(v))) #list of cumsums
sums = [j if i==0 else i for i,j in zip(reversed(v), acc)] #switch to cumsum where 0 exists in list
out = list(reversed(sums)) #reverse it back
print(out)
[36, 5, 5, 26, 6, 6, 14, 7, 7]
1) No
2) Neither
3) Arrays in C and C++ are zero-based.
4) Yes.
Arrays of all forms in C++ are zero-based. I.e. their index start at zero and goes up to the size of the array minus one. For example an array of five elements will have the indexes 0 to 4 (inclusive).
That is why most loops in C++ are starting at zero.
As for your specific list of questions, for 1 there might be a performance difference. If you start a loop at 1 then you might need to subtract 1 in each iterator if you use the value as an array index. Or if you increase the size of the arrays then you use more memory.
For 2 it really depends on what you're iterating over. Is it over array indexes, then the loop starting at zero is clearly better. But you might need to start a loop at any value, it really depends on what you're doing and the problem you're trying to solve.
For 3, what you need to consider is what you're using the loop for.
And 4, maybe a little. ;)
One of a number of ways would be to iterate not over your list, but over a new list that includes zero at the beginning, and then your list. Like so:
mylist=[1,2,3]
for i in [0] + mylist:
print(i, end="")
If the input list is not under your control and you neither want to prepend 0 to it nor to build a new concatenated list (e.g. if it is very large), you can use itertools.chain():
import itertools
mylist = [1, 2, 3]
for i in itertools.chain([0], mylist):
pass # Your loop code...
