Home > Net >  how can I iterate every index number in list?
how can I iterate every index number in list?

Time:09-08

list1 = [2,5,3]
for i in range(list1[]):
    print(i)

Desired Output:

0
1
0
1
2
3
4
0
1
2

I tried but I'm not able to iterate the indexes of list1.

CodePudding user response:

So do it by step :

  • for each value in the list
    • generate the values from 0 to the value (excluded)

list1 = [2,5,3]
for value in list1:
    for i in range(value):
        print(i)

CodePudding user response:

you need to print the from 0 to element-1 for each element in the list. use two loop, one to iterate on origanl list which give the element and second one to print element from 0 to element -1 respectively

list1 = [2,5,3]
for element in list1:
    for j in range(element):
        print(j)

CodePudding user response:

You can just print range(i) values for every i in list1

list1 = [2, 5, 3]
for i in list1:
    print(*range(i), sep='\n')
  • Related