-1

I have the list

lst = [3, 5]

How to get a range for each item in the list like this ?

[0, 1, 2, 3]
[0, 1, 2, 3, 4, 5]

This is loop is obviously not working

for i in range(0, lst):
  print(i)
hug
  • 247
  • 4
  • 14

2 Answers2

1
lst = [3, 5]
[list(range(x+1)) for x in lst]
#[[0, 1, 2, 3], [0, 1, 2, 3, 4, 5]]

Or simply:

for x in lst:
    print(list(range(x+1)))
[0, 1, 2, 3]
[0, 1, 2, 3, 4, 5]
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
0

Call range() for each end; to include the end in the range, add 1:

>>> lst = [3, 5]
[3, 5]
>>> ranges = [range(end + 1) for end in lst]
[range(0, 4), range(0, 6)]
>>> for x in ranges:
...     print(list(x))
...
[0, 1, 2, 3]
[0, 1, 2, 3, 4, 5]
>>>
AKX
  • 152,115
  • 15
  • 115
  • 172