23
loading...
This website collects cookies to deliver better user experience
test = list(range(53))
test[53]
IndexError: list index out of range
--------------------------------------------------------------------------------
IndexError Traceback (most recent call last)
c:\Projects\Tryouts\listindexerror.py in
1 test = list(range(53))
---------> 2 test[53]
IndexError: list index out of range
range()
*to iterate over a list.numbers = [1, 2, 3, 4, 6, 8, 10]
index = 3
if index < len(numbers):
print(numbers[index])
#Output 4
numbers = [1, 2, 3, 4, 6, 8, 10]
for i in numbers:
print(i)
range()
function returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and stops before a specified number. The range function will help in iterating items in the for a loop.numbers = [1, 2, 3, 4, 6, 8, 10]
for i in range(len(numbers)):
print(i)