Reputation: 203
I have got some data in the format label + numpy array like
x = ('red', array([ 0., 1., 0., ..., 0., 0., 0.]))
and a list[[]] and a loop that goes through and based on some criteria it should put each of these in a list within list. I tried list[somevalue].append(x) but when somevalues > 0 I get an error IndexError: list index out of range. What did I wrong?
Also, I would need to be able to access the numpy array from within the sublist later on to do some processing. Can anyone help me?
Upvotes: 0
Views: 266
Reputation: 69192
You have a list l
that contains exactly one element (and this element happens to be another list, but that's not highly relevant here), therefore:
l[0] # WORKS: this is the first item in the list
l[1] # INVALID: there is no item here, so it raised an IndexError
This is the basic answer that everyone else is saying, and I'm just trying to say it very clearly. Note, specifically, that Python lists are indexed with the first element at 0
rather than 1
, so a list of length 1 will only have a single element at index 0.
A way that you could have more easily identified the problem yourself would have been to split the compound line you use into two lines -- this is a common method of debugging which is generally very useful:
# l[1].append(x) # doing two things in the same line (accessing and element, and then appending)
temp = l[1]
temp.append(x)
Here the traceback will point more clearly to the problem.
(As an aside, I disagree with dpkp's suggestion of using a "defaultlist" since I think it's an overly complicated, non-standard, and advanced solution to a beginner problem. Instead, if you want a workaround to this problem where a list is automatically created to append to, please post that as a separate question with more specifics about why you want it -- and I think you'd probably want a defaultdict using integer keys, but that should wait for the full question.)
Upvotes: 1
Reputation: 12316
It sounds like you are saying: if there is already a sublist in my list L
at position somevalue
, then append x
to it. If not, create a new list like L[somevalue]
with value x
. If this is the case then you can't reference those non-existing elements directly without other elements already in place.
If somevalue
is always the next element in the list 0,1,2
then could could make it work with a list, but if it could be 5
when only elements 0,1
exist, then you would could use a dictionary instead. This lets you refer to D[5]
and generate items non-sequentially that don't already exist.
D={}
D[somevalue] = D.get(somevalue,[]) + [x]
If you use +[x]
with .get()
it will avoid the errors of trying to .append()
to a NoneType
object.
Upvotes: 1
Reputation: 1459
without posting more specific code, it's hard to help out. but based on your description the specific problem you are facing is that you cannot get the value of a list using an index that does not yet exist. If you start with [ [] ] then you have a list with only one element, which itself is a list with no elements. so 0 would be the only index you could access via list[somevalue] . Sounds like you want a defaultdict (see collections module), or a defaultlist: creating a defaultlist in python
Upvotes: 0