Home > Net >  Range function not iterating whole list, help me pls
Range function not iterating whole list, help me pls

Time:04-24

def count4(lst):
    count = 0
    for i in range(lst[0],lst[-1] 1):
        print(i)
      

print(count4([1,2,3,4,5,6,4,5,4,4]))

Here the output is showing just "1234" and not the whole list pls tell me how to iterate this list using range function.

CodePudding user response:

The reason you are getting output as "1234"

The below statement

for i in range(last[0], last[-1] 1):

is interpreted as

for i in range(1, 4   1):

i.e

for i in range(1,5):

Solution: Use this

 for i in lst:

CodePudding user response:

You were trying to iterate in the range of values stored in the list at starting and end position that was passed, which is logically incorrect.

def count4(lst):
    for i in range(len(lst)):
        print(lst[i])
      
print(count4([1,2,3,4,5,6,4,5,4,4]))
  • Related