Incorrect line number returned on specifying Start parameter in enumerate Python
<p>I have slight confusion regarding the start parameter in enumerate function,as i recently started working on python i don't have much idea how it is supposed to work.
Suppose i have an example file below:</p>

<pre><code>Test 1
Test 2
Test 3
</code></pre>

<p>This is the first line <code>[SB WOM]|[INTERNAL REQUEST]|[START]</code> which is the start of message</p>

<pre><code>Name : Vaibhav
Designation : Technical Lead
ID : 123456
Company : Nokia
</code></pre>

<p>This is the sixth line <code>[SB WOM]|[INTERNAL REQUEST]|[END]</code> which is the end of message</p>

<p>Now when i run the below code :</p>

<pre><code>path =("C:/Users/vgupt021/Desktop")
in_file = os.path.join(path,"KSClogs_Test.txt")
fd = open(in_file,'r')
for linenum,line in enumerate(fd) :
    if "[SB WOM]|[INTERNAL REQUEST]|[START]" in line:
        x1 = linenum
        print x1
        break 
for linenum,line in enumerate(fd,x1):
    if "[SB WOM]|[INTERNAL REQUEST]|[END]" in line:
        print linenum
        break
</code></pre>

<p>I get the linenum returned as 3 and 7, I am not clear why it is not returned as 3,8.Since the index number of line <code>"[SB WOM]|[INTERNAL REQUEST]|[END]"</code> is 8 and not 7, how the start parameter makes the difference in second part of the loop.</p>

<p>Try this code</p>

<pre><code>x = range(10)
for i, e in enumerate(x):
    if i == 4:
        print i
        st = i
        break

for i, e in enumerate(x, st):
    print i
</code></pre>

<p>And you will see this output:</p>

<pre><code>4
4 5 6 7 8 9 10 11 12 13
</code></pre>

<p>So, what does the second parameter of <code>enumerate</code>? Well, it's the starting value of the index of <code>enumerate</code>. The iterable variable <code>x</code> is enumerated again from the beginning but the values of <code>i</code> at different iteration is shifted by the value of <code>st</code>.</p>

<p>Instead of having the values of <code>i</code> as 0, 1, 2, etc., we have 4, 5, 6, etc.</p>

<p>I think that explains why you have the incorrect line number in your code.</p>

python