KMeta
KMeta

Reputation: 11

Python split OR loop OR str function running outofIndex dealing with big list - works okay with shorter list

I used simple string conversion function from list (dict actually) and string function runs out of index due (it ends up with None for one of the input). Below function call kills it when it encounters bad string

test_s=s_i.split("Inv/", 1)[1].split(">",1)[0]

Q1) My loop breaks at split, and splits spits out list Index out of range error. How do I make it skip this string, display a warning, still continue to concatenate strings with +?
OR can you please help suggest better method, to do this split?

Q2) How to I troubleshoot split? (i.e. how do I debug this)

Q3) Also, how do I input (prompt a python user input prompt) the same list in python?

Below is the code i am trying to run: Sample input - it is actually list of 100+ items:

a_dict={'1234': <Batman:/Inv/Batman/xyzhash>,'4567': <Superman:/Inv/Superman/xyzhash, ..100 more items>

test_f=""
test_s=""
s_i=""

for ind_values in a_dict.values():
    s_i=str(ind_values)  
    test_s=s_i.split("Inv/", 1)[1].split(">",1)[0]
    test_f +=test_s+","

s_i=str(ind_values) is the line number 26, but i think it's blowing on test_s=s_i.split("Inv/", 1)[1].split(">",1)[0].

Error message:
Traceback (most recent call last):
  File "<string>", line 26, in <module>
IndexError: list index out of range

Upvotes: 0

Views: 140

Answers (1)

Karl Knechtel
Karl Knechtel

Reputation: 61625

s_i=str(ind_values) is the line number 26.

No, it is not. The error is on the next line:

test_s=s_i.split("Inv/", 1)[1].split(">",1)[0]

You can tell because the error message says that a list index is out of range, and this is the line where you attempt to use an index into a list - in two places.

The first one is the issue: s_i.split("Inv/", 1)[1]. If "Inv/" does not appear in the string, then the .split call will produce a list with only one item, which has only a [0] index and not a [1] index. The 1 parameter to .split only specifies a maximum number of splits, not a required number.

Upvotes: 1

Related Questions