Python Forum
IndexError: list assignment index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: list assignment index out of range
#1
This seems to be something that a lot of new people to python seem to struggle with, and I'm no exception....

I understand that appending the values is something I need to do, but.. I'm obviously not doing it QUITE right..

This is my code:
x=0
dist=[]
while x < 100:
dist.append(x)
dist[x]=400/(math.cos(angle))
print (dist[x])
x = x+1
Where angle comes from earlier in the code. But I still get the list assignment out of range error. What am I doing wrong?
Reply
#2
Hi,

I don't get that error because indentation is missing from the code, math has not been imported and angle has not been defined. when asking questions please show code that can be run, that will give the error, also please post the full error trace back in error tags.

For what you seem to be doing a for loop would be easier
import math

angle = 20

dist=[]

for x in range(100):
    calculation = 400/(math.cos(angle))
    dist.append(calculation)
    print(calculation)
i assume the value of x is supposed to be in the calculation somewhere, because it currently just outputs
Output:
980.1950083826823
100 times.
Reply
#3
Brilliant thank you.

Apologies, there was a long code beforehand which gives the angle value, which I knew to be working, so didn't include.

So, if I am to understand correctly, the append function adds another value to the list (in this case dist), which is the value in the brackets. Now makes sense.
Reply
#4
Yes it is appending calculation to the end of the sequence.
my_list = []
print(my_list)
my_list.append('an item')
print(my_list)
my_list.append('another item')
print(my_list)
Output:
[] ['an item'] ['an item', 'another item']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  removing one list element without using its index paul18fr 7 1,139 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
  IndexError: index 31 is out of bounds for axis 0 with size 31 YL789 1 833 Sep-21-2024, 09:46 AM
Last Post: Gribouillis
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 9,441 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 8,523 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 13,665 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 3,327 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 3,830 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 2,199 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 4,431 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 6,398 Nov-21-2021, 08:40 AM
Last Post: paul18fr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020