Reputation: 305
I have the following array(in python):
arr= ['\n', ' a#B\n', ' c#D\n']
Now I an trying to split on '#' for which I am writing the code given below:
for i in arr:
g=i.split("#")[1]
I am getting the following error:
g=i.split("#")[1]
IndexError: list index out of range
I am not getting why am I getting the error. Can someone be kind enough to help rectify it.
Upvotes: 0
Views: 159
Reputation: 143047
The error is caused because the first split results in a list that contains only 1 element ['\n']
, so the index value of [1]
is out of bounds.
See:
for i in arr:
g = i.split("#")
print g
output:
['\n']
[' a', 'B\n']
[' c', 'D\n']
One quick way to fix it would be:
[i.split('#')[1] for i in arr if len(i.split('#')) > 1]
though it contains two calls to split()
.
The one below is a bit more verbose, but only calls split()
once.
for i in arr:
g = i.split('#')
if len(g) > 1:
g = g[1]
As @kindall suggests below in the comments, the use of partition()
is an alternative to split()
here too.
Upvotes: 1
Reputation: 36474
Others have explained why you're getting that exception. Here's an idea for a different approach that may be useful to you.
Starting with Python 2.5, string objects grew a new partition
method:
str.partition(sep)
Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.
>>> arr= ['\n', ' a#B\n', ' c#D\n']
>>> for s in arr:
... print s.partition('#')
...
('\n', '', '')
(' a', '#', 'B\n')
(' c', '#', 'D\n')
You can pull the string apart without encountering an excpetion whether the separator is found or not.
Upvotes: 2
Reputation: 11585
The first element in your array that you iterate through has no hash #
so when you call split on it, it returns only 1 item in the list as in ['\n']
so when you try to access the second item by [1]
it can't find it giving you the error.
Remember, in Python indexing starts at 0, not 1.
Upvotes: 0
Reputation: 47978
When the first element of your list, '\n'
is assigned to i
it does not contain "#"
, so the result list is only one element long, not two. Attempting to retrieve index 1
fails, but index 0
would be present.
Upvotes: 2