For loop related questions

Pl tell me th meaning of this for loop
Like i hv a list
Lst =[1,2,3,4,5]
For i in lst(:):
&
for i in lst:

@sayanalu9091

for i in lst: depicts list allows you to iterate over a given list of pairs of elements (list of tuples or list of lists)

Lets consider the example of an array: -

for i in arr:
    print(arr[i])

for i in arr will give you the values of each item in the array. So, what you are doing then is actually putting in a value in to arr as if it is the index. But it is not.

When you do this:

for i in range(0, len(arr))

What is happening here is you are getting an incremental value that is based on the size of the array, so that way you can use that value as an index to your list.

For i in lst(:): depicts the range(), by which you can access list/tuple values through index

1 Like