'Range function not iterating whole list, help me pls

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.



Solution 1:[1]

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:

Solution 2:[2]

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. The other suggested methods are also correct since you specifically said to use the range function so think this might be the answer you were looking for

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Akash garg
Solution 2