0

I got this error while running some code.

Traceback (most recent call last):
  File "C:/Users/jakus/PycharmProjects/practice/Practice.py", line 16, in <module>
    x = x_values.pop(x)
IndexError: pop index out of range

Here is the code.

x_values = [1, 2, 3, 4, 5]
y_values = [6, 7, 8, 9, 10]

for x in x_values:
    x = x_values.pop(x)
    x == x**2
    x_values.append(x)

for y in y_values:
    y = y_values.pop(y)
    y == y**2
    y_values.append(y)

print(y_values)
print(x_values)

I am not sure how the pop index is out of range if it is currently working on that same variable in my for loop. I would appreciate any help that can be provided.

Top Of Tech
  • 305
  • 2
  • 11

1 Answers1

1

Don't modify the size of a list as you're iterating over it. What you should do here instead is a list comprehension.

x_values = [x**2 for x in x_values]

BTW you had a typo, == instead of =.

wjandrea
  • 28,235
  • 9
  • 60
  • 81