Hi , so now suppose you have an list in python :

1 |
list=[2,3,4,5,69,10] |

you want to add 1 to each element of list so if you try like this :

1 2 3 |
list=[2,3,4,5,69,10] for i in range(len(list)): print (i+1) |

You will get this output:

1 2 3 4 5 6 7 8 |
1 2 3 4 5 6 7 8 |

Because its adding 1 to each iterator value.

First what is i here ?

So first let’s print i and know what is exactly i is here ?

1 2 3 |
list=[2,3,4,5,69,10] for i in range(len(list)): print (i) |

output

1 2 3 4 5 6 7 8 |
0 1 2 3 4 5 6 7 |

i is iterator it means it will start from 0,1,2,3,4 till length of list if we give statement like :

1 |
for i in range(len(list)): |

Now that’s why (i+1) will add 1 to each iterator now to value of list.

Now try something like this :

1 2 3 |
list=[2,8,6,8,6,4,9,4] for i in range(len(list)): print ("iterator value is {0} and list index value is {1} and sum is index value {1} + 1 which is {2}".format(i,list[i],list[i]+1)) |

it will add “1” to each value of index value :

output:

1 2 3 4 5 6 7 8 |
iterator value is 0 and list index value is 2 and sum is index value 2 + 1 which is 3 iterator value is 1 and list index value is 8 and sum is index value 8 + 1 which is 9 iterator value is 2 and list index value is 6 and sum is index value 6 + 1 which is 7 iterator value is 3 and list index value is 8 and sum is index value 8 + 1 which is 9 iterator value is 4 and list index value is 6 and sum is index value 6 + 1 which is 7 iterator value is 5 and list index value is 4 and sum is index value 4 + 1 which is 5 iterator value is 6 and list index value is 9 and sum is index value 9 + 1 which is 10 iterator value is 7 and list index value is 4 and sum is index value 4 + 1 which is 5 |

You have to know

1 i is iterator value and list[i] is index value

Now lets try something more to clear this doubt :

1 2 |
list=[2,8,6,8,6,4,9,4] op=list[1]+list[4] |

what do you expect ?

If you are thinking it will add one and 4 and then print list[5] value then you are wrong it will add index 1 value to index 4 value :

index 1 value : 8

index 6 value: 6

list[1]+list[4] is 14

take a look of this example too:

1 2 3 4 5 6 7 |
k=[1]+[2] print(k) #output print(k) [1,2] |

and now this :

1 2 3 4 5 6 7 8 |
list=[2,8,6,8,6,4,9,4] jp=list[3+1] print(jp) #output 6 |

now its not adding with index value its adding 1 to iterator value and then it will print [3+1] =list[4] element

if you try this:

1 2 3 4 5 6 |
list=[2,8,6,8,6,4,9,4] for i in range(len(list)): print (list[i+1]) |

it will give error :

1 2 3 4 5 6 7 8 9 |
print (list[i+1]) IndexError: list index out of range 8 6 8 6 4 9 4 |

why? because as i said “i” is iterator value so its adding one to iterator and then printing that no of index

what we have:

so first list[i+1[ will print list[0+1] which is list[1] value 8

second it will add [1+1] which is list [2] value 6

———

———-

———

at last it will add [7+1] which is list[8] but there is no 8th element in list.

Now here is the trick on which this post is based on :

You can remove error by try and catch exception but i am going to tell little trick:

whenever you need to add in iterator in loop then just subtract one in loop argument i mean always iterate less than len of list so what i meant is :

1 2 3 4 |
list = [2, 8, 6, 8, 6, 4, 9, 4] for i in range(len(list)-1): print(list[i + 1]) |

you can see i have did something (len(list)-1 which is iterating less than one of total index of list now if we run we will not get error :

output :

1 2 3 4 5 6 7 |
8 6 8 6 4 9 4 |

because it will always iterate less one then the size of list.

suppose list is list=[1,2]

if we don’t subtract then when it iterate two times which iterate values 0 and 1 for second iteration i=1 so it will try to find [i+1] which is index[2] and there is no [2] in list and if we subtract one then it will iterate one time which is i=0 and now [i+1] which is i[0+1] so it will always in limit .

we can generate list with range function:

1 2 |
primes = list(range(100)) print(primes) |

another trick about return and calling function . see this example in example i am not calling function simply i am calling function with return statement :

1 2 3 4 5 6 7 8 9 |
def hello(x,b): z=x+b print(z) return "hi" def hi(n): return hello(4,4) print(hi(3)) |

Let’s talk about nested loops :

1 2 3 |
for i in range(1,10): for j in range(0,i): print ("i",i) |

for each i it will iterate in range from 0 to i

it will check each element of i with each value between 0 to i