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
I am trying to create a multiple dimension array with predefined index:
    returns = []
    for s in context.securities:
        returns[s] = []
    print(returns)
but the above code returns
Quote:IndexError: list assignment index out of range

Thank you!

Ted
Reply
#2
you don't explain what context.securities looks like
This what I think you want
returns = []
for s in context.securities:
    returns.append([])
print(returns)
Reply
#3
Larz is right that you don't explain what context.securities looks like and what the solution is. I just want to explain the cause of the error.
List is mutable object and returns[s] = [] is the syntax for changing element at index s. However returns is empty list so, whatever the value of s is, there is no element at index s (i.e. index value s is out of the range of available indexes, which is in fact empty range)

>>> returns = []
>>> returns[0] = 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> returns.append(10)
>>> returns
[10]
>>> returns[0] = 1
>>> returns
[1]
>>>
Reply
#4
Sorry for the lack of information:
context.securities looks like this:
PRINT [Equity(24 [AAPL]), Equity(16841 [AMZN])]
returns = []
for s in context.securities:
    returns.append([])
print(returns)
Seems to be fine for creating the array for each element in the context.securities, but I wonder how can I create the index of s in the returns list so that I can call print(returns[s]) when I iterate through each?

Thank you so much!
Reply
#5
well,
PRINT [Equity(24 [AAPL]), Equity(16841 [AMZN])]
is (i) not a valid python code and (ii) - not something you can iterate over
I take my words back - [Equity(24 [AAPL]), Equity(16841 [AMZN])] looks like a list of custom class objects. However it is not clear how you derive returns for each security, e.g. Equity(24 [AAPL])
what external package do you use to produce context.securities
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 1,947 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 3,154 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,954 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,843 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,277 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,408 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,499 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,101 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,759 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,233 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav

Forum Jump:

User Panel Messages

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