0

So i want to pop all the '10' from the num_list, but i get this IndexError on line 5 saying that, IndexError: list index out of range.

is_10=[]
num_list = [422, 136, 524, 85, 96, 719, 85, 92, 10, 17, 312, 542, 87, 23, 86, 191, 116, 35, 173, 45, 149, 59, 84, 69, 113, 166]
print(len(num_list)) #answer is 26

for i in range(26):
    if num_list[i] == 10:
        is_10.append(num_list.pop(i))
print(is_10)

"Traceback (most recent call last):
File "<stdin>", line 5, in <module>
IndexError: list index out of range"

Whereas if i change to range(25) instead, it runs without error

is_10=[]
num_list = [422, 136, 524, 85, 96, 719, 85, 92, 10, 17, 312, 542, 87, 23, 86, 191, 116, 35, 173, 45, 149, 59, 84, 69, 113, 166]
print(len(num_list)) #answer is 26

for i in range(25):
    if num_list[i] == 10:
        is_10.append(num_list.pop(i))
print(is_10)
"no error"
timgeb
  • 76,762
  • 20
  • 123
  • 145

0 Answers0