I have a python code that contains a list dict, where each element is a list of arbitrary size. I am looping over the elements as follows:
for i in range(len(dict)):
for j in range(1,len(dict[i])):
str = dict[i][j]
at this point, I get an error saying IndexError: list index out of range. I am using range(1,len(dict)) because I want to skip the first element of each list in dict. At the point the error is generated, i=5, len(dict[5])=2, so j should be looping over only 1, but when I check the j value, I get 2. How is this possible?
What's even weirder is that when I type the above code in the python console, I get no such error and everything works fine.
Edit: the full code is: (note the change from dict to keywords)
import re
conds = [['emerald cryo i&ii,a,01', '40% (v/v) mpd', 'sodium phosphate dibasic', 'citric acid'],['emerald cryo i&ii,a,02', '40% (v/v) ethylene glycol', 'sodium acetate', 'acetic acid'],['emerald cryo i&ii,a,03', '50% (v/v) peg-200', 'citrate', 'na']]
keywords = [["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"],["'rbcl+mgcl2'", " 'rbcl+mgcl2 (0.025m each)'"]]
#cycle through elements to see if there is a match to the dictionary
for i in range(len(keywords)):
for j in range(1,len(keywords[i])):
print j
for k in range(len(conds)):
str = keywords[i][j].strip().strip("'").strip() #this is where the error occurs
match = [(str == l) for l in conds[k]]
ind = [i for i, x in enumerate(match) if x]
if len(ind) !=0:
print ind
print str
The actual conds and keywords lists are lot longer and being read in from a file, but I just copied and pasted two elements each from the python console.
Edit 2: printed out i, j, len(dict[i]), dict[i] in the inner loop. The output is too long to put here, but here is a condensed version:
0 1 3 ["'potassium acetate'", " 'k(oac)'", " 'potassium acetate'"]
3 1 3 ["'ammonium nitrate'", " '(nh4)2(no)3'", " 'ammonium nitrate'"]
3 1 3 ["'ammonium nitrate'", " '(nh4)2(no)3'", " 'ammonium nitrate'"]
3 1 3 ["'ammonium nitrate'", " '(nh4)2(no)3'", " 'ammonium nitrate'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
5 1 2 ["'rbcl+mgcl2'", " 'rbcl+mgcl2 (0.025m each)'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
3 1 3 ["'ammonium nitrate'", " '(nh4)2(no)3'", " 'ammonium nitrate'"]
3 1 3 ["'ammonium nitrate'", " '(nh4)2(no)3'", " 'ammonium nitrate'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
[2]
sodium acetate
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
5 1 2 ["'rbcl+mgcl2'", " 'rbcl+mgcl2 (0.025m each)'"]
...
5 2 2 ["'rbcl+mgcl2'", " 'rbcl+mgcl2 (0.025m each)'"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "coarseCondEdit.py", line 38, in <module>
str = keywords[i][j].strip().strip("'").strip()
IndexError: list index out of range
dict. Why?dicta list? This is confusing. Show yourdictcontents perhaps.dictisn't a reserved word that can't be used as a variable name, and neither isstr, but using them is still a bad idea, behind that hides the names of the types. If you later want to writes = str(i)ord = dict(*pairs), you're going to get a confusing error because you're trying to call your string object or list object instead of thestrordictconstructors. And, of course, you're confusing everyone who reads your code, and making them have to think twice as hard to follow it, which is a bad idea when you want people to find a problem for you.