Why am I getting a list index error when there are certainly items in the list at the specific index?
I am working on some Code Signal practice problems in Python 2 and I have been running into the same error a lot. Like the title of the question says, I can not access a specific list element even though i am sure it exists. When I try to run the code shown below I get this error, IndexError: list index out of range. Shown below is some code I am working with. Any help is appreciated, i'm sure this is a simple misunderstanding of how Python2 works. In this example
s = "adobecodebanc" and t is not relevant for my question
def minSubstringWithAllChars(s, t):
nuS = str(s)
listS = list(nuS)
for i in range(len(listS)):
print i
print listS[i]
print listS[0]
When I omit the last line of code I am able to print all of the characters that are in S - as shown below. However, when I add the last line, I am unable to print anything as a I get an indexing error.
0
a
1
d
2
o
3
b
4
e
5
c
6
o
7
d
8
e
9
b
10
a
11
n
12
c
What could this be due to? Here is the text of my error message
Traceback (most recent call last):
file.py on line ?, in getUserOutputs
userOutput = _runjodsn(testInputs[i])
file.py on line ?, in _runjodsn
return minSubstringWithAllChars(*_fArgs_nycavblqytqu)
file.py on line 9, in minSubstringWithAllChars
print listS[0]
IndexError: list index out of range
python python-2.x
add a comment |
I am working on some Code Signal practice problems in Python 2 and I have been running into the same error a lot. Like the title of the question says, I can not access a specific list element even though i am sure it exists. When I try to run the code shown below I get this error, IndexError: list index out of range. Shown below is some code I am working with. Any help is appreciated, i'm sure this is a simple misunderstanding of how Python2 works. In this example
s = "adobecodebanc" and t is not relevant for my question
def minSubstringWithAllChars(s, t):
nuS = str(s)
listS = list(nuS)
for i in range(len(listS)):
print i
print listS[i]
print listS[0]
When I omit the last line of code I am able to print all of the characters that are in S - as shown below. However, when I add the last line, I am unable to print anything as a I get an indexing error.
0
a
1
d
2
o
3
b
4
e
5
c
6
o
7
d
8
e
9
b
10
a
11
n
12
c
What could this be due to? Here is the text of my error message
Traceback (most recent call last):
file.py on line ?, in getUserOutputs
userOutput = _runjodsn(testInputs[i])
file.py on line ?, in _runjodsn
return minSubstringWithAllChars(*_fArgs_nycavblqytqu)
file.py on line 9, in minSubstringWithAllChars
print listS[0]
IndexError: list index out of range
python python-2.x
Could you please include the text of your error message (stack trace)?
– MyStackRunnethOver
Nov 28 '18 at 22:44
No problem. I added it at the bottom of my question
– Kbeher
Nov 29 '18 at 1:26
Instead of printing listS[0], try toprint(listS)
just to see how the list looks like before you try and access it via index.
– Alexandre Juma
Nov 29 '18 at 2:12
It looks just like the list you would expect to see. Here is the what I printed out when i printed the entire list. ['a', 'd', 'o', 'b', 'e', 'c', 'o', 'd', 'e', 'b', 'a', 'n', 'c']. This shows that there is an item in the index 0 of listS. So i'm not sure what's going on!
– Kbeher
Nov 29 '18 at 8:06
I think it's the platform you're using. If you see the stacktrace, it's clearly not only executing what you've shown here. There are other intermediate calls and even the arguments to minSubstringWithAllChars are passed as non-keyworded variable length argument (*_fArgs_nycavblqytqu) and not formal args we're testing here. Also a stacktrace of a Indexerror with that code would produce this stacktrace:File "main.py", line 11, in <module> minSubstringWithAllChars("abe", "") File "main.py", line 9, in minSubstringWithAllChars print listS[7] IndexError: list index out of range
– Alexandre Juma
Nov 29 '18 at 10:20
add a comment |
I am working on some Code Signal practice problems in Python 2 and I have been running into the same error a lot. Like the title of the question says, I can not access a specific list element even though i am sure it exists. When I try to run the code shown below I get this error, IndexError: list index out of range. Shown below is some code I am working with. Any help is appreciated, i'm sure this is a simple misunderstanding of how Python2 works. In this example
s = "adobecodebanc" and t is not relevant for my question
def minSubstringWithAllChars(s, t):
nuS = str(s)
listS = list(nuS)
for i in range(len(listS)):
print i
print listS[i]
print listS[0]
When I omit the last line of code I am able to print all of the characters that are in S - as shown below. However, when I add the last line, I am unable to print anything as a I get an indexing error.
0
a
1
d
2
o
3
b
4
e
5
c
6
o
7
d
8
e
9
b
10
a
11
n
12
c
What could this be due to? Here is the text of my error message
Traceback (most recent call last):
file.py on line ?, in getUserOutputs
userOutput = _runjodsn(testInputs[i])
file.py on line ?, in _runjodsn
return minSubstringWithAllChars(*_fArgs_nycavblqytqu)
file.py on line 9, in minSubstringWithAllChars
print listS[0]
IndexError: list index out of range
python python-2.x
I am working on some Code Signal practice problems in Python 2 and I have been running into the same error a lot. Like the title of the question says, I can not access a specific list element even though i am sure it exists. When I try to run the code shown below I get this error, IndexError: list index out of range. Shown below is some code I am working with. Any help is appreciated, i'm sure this is a simple misunderstanding of how Python2 works. In this example
s = "adobecodebanc" and t is not relevant for my question
def minSubstringWithAllChars(s, t):
nuS = str(s)
listS = list(nuS)
for i in range(len(listS)):
print i
print listS[i]
print listS[0]
When I omit the last line of code I am able to print all of the characters that are in S - as shown below. However, when I add the last line, I am unable to print anything as a I get an indexing error.
0
a
1
d
2
o
3
b
4
e
5
c
6
o
7
d
8
e
9
b
10
a
11
n
12
c
What could this be due to? Here is the text of my error message
Traceback (most recent call last):
file.py on line ?, in getUserOutputs
userOutput = _runjodsn(testInputs[i])
file.py on line ?, in _runjodsn
return minSubstringWithAllChars(*_fArgs_nycavblqytqu)
file.py on line 9, in minSubstringWithAllChars
print listS[0]
IndexError: list index out of range
python python-2.x
python python-2.x
edited Nov 29 '18 at 1:41
Deduplicator
34.8k65090
34.8k65090
asked Nov 28 '18 at 22:13
KbeherKbeher
11
11
Could you please include the text of your error message (stack trace)?
– MyStackRunnethOver
Nov 28 '18 at 22:44
No problem. I added it at the bottom of my question
– Kbeher
Nov 29 '18 at 1:26
Instead of printing listS[0], try toprint(listS)
just to see how the list looks like before you try and access it via index.
– Alexandre Juma
Nov 29 '18 at 2:12
It looks just like the list you would expect to see. Here is the what I printed out when i printed the entire list. ['a', 'd', 'o', 'b', 'e', 'c', 'o', 'd', 'e', 'b', 'a', 'n', 'c']. This shows that there is an item in the index 0 of listS. So i'm not sure what's going on!
– Kbeher
Nov 29 '18 at 8:06
I think it's the platform you're using. If you see the stacktrace, it's clearly not only executing what you've shown here. There are other intermediate calls and even the arguments to minSubstringWithAllChars are passed as non-keyworded variable length argument (*_fArgs_nycavblqytqu) and not formal args we're testing here. Also a stacktrace of a Indexerror with that code would produce this stacktrace:File "main.py", line 11, in <module> minSubstringWithAllChars("abe", "") File "main.py", line 9, in minSubstringWithAllChars print listS[7] IndexError: list index out of range
– Alexandre Juma
Nov 29 '18 at 10:20
add a comment |
Could you please include the text of your error message (stack trace)?
– MyStackRunnethOver
Nov 28 '18 at 22:44
No problem. I added it at the bottom of my question
– Kbeher
Nov 29 '18 at 1:26
Instead of printing listS[0], try toprint(listS)
just to see how the list looks like before you try and access it via index.
– Alexandre Juma
Nov 29 '18 at 2:12
It looks just like the list you would expect to see. Here is the what I printed out when i printed the entire list. ['a', 'd', 'o', 'b', 'e', 'c', 'o', 'd', 'e', 'b', 'a', 'n', 'c']. This shows that there is an item in the index 0 of listS. So i'm not sure what's going on!
– Kbeher
Nov 29 '18 at 8:06
I think it's the platform you're using. If you see the stacktrace, it's clearly not only executing what you've shown here. There are other intermediate calls and even the arguments to minSubstringWithAllChars are passed as non-keyworded variable length argument (*_fArgs_nycavblqytqu) and not formal args we're testing here. Also a stacktrace of a Indexerror with that code would produce this stacktrace:File "main.py", line 11, in <module> minSubstringWithAllChars("abe", "") File "main.py", line 9, in minSubstringWithAllChars print listS[7] IndexError: list index out of range
– Alexandre Juma
Nov 29 '18 at 10:20
Could you please include the text of your error message (stack trace)?
– MyStackRunnethOver
Nov 28 '18 at 22:44
Could you please include the text of your error message (stack trace)?
– MyStackRunnethOver
Nov 28 '18 at 22:44
No problem. I added it at the bottom of my question
– Kbeher
Nov 29 '18 at 1:26
No problem. I added it at the bottom of my question
– Kbeher
Nov 29 '18 at 1:26
Instead of printing listS[0], try to
print(listS)
just to see how the list looks like before you try and access it via index.– Alexandre Juma
Nov 29 '18 at 2:12
Instead of printing listS[0], try to
print(listS)
just to see how the list looks like before you try and access it via index.– Alexandre Juma
Nov 29 '18 at 2:12
It looks just like the list you would expect to see. Here is the what I printed out when i printed the entire list. ['a', 'd', 'o', 'b', 'e', 'c', 'o', 'd', 'e', 'b', 'a', 'n', 'c']. This shows that there is an item in the index 0 of listS. So i'm not sure what's going on!
– Kbeher
Nov 29 '18 at 8:06
It looks just like the list you would expect to see. Here is the what I printed out when i printed the entire list. ['a', 'd', 'o', 'b', 'e', 'c', 'o', 'd', 'e', 'b', 'a', 'n', 'c']. This shows that there is an item in the index 0 of listS. So i'm not sure what's going on!
– Kbeher
Nov 29 '18 at 8:06
I think it's the platform you're using. If you see the stacktrace, it's clearly not only executing what you've shown here. There are other intermediate calls and even the arguments to minSubstringWithAllChars are passed as non-keyworded variable length argument (*_fArgs_nycavblqytqu) and not formal args we're testing here. Also a stacktrace of a Indexerror with that code would produce this stacktrace:
File "main.py", line 11, in <module> minSubstringWithAllChars("abe", "") File "main.py", line 9, in minSubstringWithAllChars print listS[7] IndexError: list index out of range
– Alexandre Juma
Nov 29 '18 at 10:20
I think it's the platform you're using. If you see the stacktrace, it's clearly not only executing what you've shown here. There are other intermediate calls and even the arguments to minSubstringWithAllChars are passed as non-keyworded variable length argument (*_fArgs_nycavblqytqu) and not formal args we're testing here. Also a stacktrace of a Indexerror with that code would produce this stacktrace:
File "main.py", line 11, in <module> minSubstringWithAllChars("abe", "") File "main.py", line 9, in minSubstringWithAllChars print listS[7] IndexError: list index out of range
– Alexandre Juma
Nov 29 '18 at 10:20
add a comment |
1 Answer
1
active
oldest
votes
You probably are running into a strange issue with your provider python compiler or maybe, since it's a page with a web application to test, run and validate python exercices, your code is not running exactly the way you see it on the web page.
This can be clearly spoted if you compare your code to the stacktrace, which has additional calls and even the arguments for your function don't seem to be standard formal arguments, but a *args (non-keyworded variable length argument) argument.
A stacktrace for an IndexError for your code (for example if your string argument was empty, would be):
Traceback (most recent call last):
File "main.py", line 11, in <module>
minSubstringWithAllChars("abe", "")
File "main.py", line 9, in minSubstringWithAllChars
print listS[0]
IndexError: list index out of range
Anyway, running With python 2.7.13 clearly shows no problem with your code:
def minSubstringWithAllChars(s):
nuS = str(s)
listS = list(nuS)
print("working with string: " + nuS)
print("Len of s: " + str(len(nuS)))
print("Len of listS: " + str(len(listS)))
print("n")
for i in range(len(listS)):
print("Checking position: " + str(i) + " of String (" + nuS + "): " + listS[i])
print("nAdditional access to listS via index 0: ")
print listS[0]
print("Startn")
minSubstringWithAllChars("adobecodebanc")
print("nEnd")
You can see it works:
Start
working with string: adobecodebanc
Len of s: 13
Len of listS: 13
Checking position: 0 of String (adobecodebanc): a
Checking position: 1 of String (adobecodebanc): d
Checking position: 2 of String (adobecodebanc): o
Checking position: 3 of String (adobecodebanc): b
Checking position: 4 of String (adobecodebanc): e
Checking position: 5 of String (adobecodebanc): c
Checking position: 6 of String (adobecodebanc): o
Checking position: 7 of String (adobecodebanc): d
Checking position: 8 of String (adobecodebanc): e
Checking position: 9 of String (adobecodebanc): b
Checking position: 10 of String (adobecodebanc): a
Checking position: 11 of String (adobecodebanc): n
Checking position: 12 of String (adobecodebanc): c
Additional access to listS via index 0:
a
End
Another thing is that you don't need to use indexes to iterate your string characters (or break your string into an array of characters with list()). You can just shortcut with this:
for i in nuS:
print i
While I'm not sure this counts as an answer, I agree that the provided code runs without issue.
– Windmill
Nov 29 '18 at 2:09
Thanks for your help. I turned the argument of the function into a string and then into a list just to ensure I was dealing with a list and not with a unicode or string. Although when i tried printing the value of s directly (s[0]) it also gave me the same error
– Kbeher
Nov 29 '18 at 8:10
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53528909%2fwhy-am-i-getting-a-list-index-error-when-there-are-certainly-items-in-the-list-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You probably are running into a strange issue with your provider python compiler or maybe, since it's a page with a web application to test, run and validate python exercices, your code is not running exactly the way you see it on the web page.
This can be clearly spoted if you compare your code to the stacktrace, which has additional calls and even the arguments for your function don't seem to be standard formal arguments, but a *args (non-keyworded variable length argument) argument.
A stacktrace for an IndexError for your code (for example if your string argument was empty, would be):
Traceback (most recent call last):
File "main.py", line 11, in <module>
minSubstringWithAllChars("abe", "")
File "main.py", line 9, in minSubstringWithAllChars
print listS[0]
IndexError: list index out of range
Anyway, running With python 2.7.13 clearly shows no problem with your code:
def minSubstringWithAllChars(s):
nuS = str(s)
listS = list(nuS)
print("working with string: " + nuS)
print("Len of s: " + str(len(nuS)))
print("Len of listS: " + str(len(listS)))
print("n")
for i in range(len(listS)):
print("Checking position: " + str(i) + " of String (" + nuS + "): " + listS[i])
print("nAdditional access to listS via index 0: ")
print listS[0]
print("Startn")
minSubstringWithAllChars("adobecodebanc")
print("nEnd")
You can see it works:
Start
working with string: adobecodebanc
Len of s: 13
Len of listS: 13
Checking position: 0 of String (adobecodebanc): a
Checking position: 1 of String (adobecodebanc): d
Checking position: 2 of String (adobecodebanc): o
Checking position: 3 of String (adobecodebanc): b
Checking position: 4 of String (adobecodebanc): e
Checking position: 5 of String (adobecodebanc): c
Checking position: 6 of String (adobecodebanc): o
Checking position: 7 of String (adobecodebanc): d
Checking position: 8 of String (adobecodebanc): e
Checking position: 9 of String (adobecodebanc): b
Checking position: 10 of String (adobecodebanc): a
Checking position: 11 of String (adobecodebanc): n
Checking position: 12 of String (adobecodebanc): c
Additional access to listS via index 0:
a
End
Another thing is that you don't need to use indexes to iterate your string characters (or break your string into an array of characters with list()). You can just shortcut with this:
for i in nuS:
print i
While I'm not sure this counts as an answer, I agree that the provided code runs without issue.
– Windmill
Nov 29 '18 at 2:09
Thanks for your help. I turned the argument of the function into a string and then into a list just to ensure I was dealing with a list and not with a unicode or string. Although when i tried printing the value of s directly (s[0]) it also gave me the same error
– Kbeher
Nov 29 '18 at 8:10
add a comment |
You probably are running into a strange issue with your provider python compiler or maybe, since it's a page with a web application to test, run and validate python exercices, your code is not running exactly the way you see it on the web page.
This can be clearly spoted if you compare your code to the stacktrace, which has additional calls and even the arguments for your function don't seem to be standard formal arguments, but a *args (non-keyworded variable length argument) argument.
A stacktrace for an IndexError for your code (for example if your string argument was empty, would be):
Traceback (most recent call last):
File "main.py", line 11, in <module>
minSubstringWithAllChars("abe", "")
File "main.py", line 9, in minSubstringWithAllChars
print listS[0]
IndexError: list index out of range
Anyway, running With python 2.7.13 clearly shows no problem with your code:
def minSubstringWithAllChars(s):
nuS = str(s)
listS = list(nuS)
print("working with string: " + nuS)
print("Len of s: " + str(len(nuS)))
print("Len of listS: " + str(len(listS)))
print("n")
for i in range(len(listS)):
print("Checking position: " + str(i) + " of String (" + nuS + "): " + listS[i])
print("nAdditional access to listS via index 0: ")
print listS[0]
print("Startn")
minSubstringWithAllChars("adobecodebanc")
print("nEnd")
You can see it works:
Start
working with string: adobecodebanc
Len of s: 13
Len of listS: 13
Checking position: 0 of String (adobecodebanc): a
Checking position: 1 of String (adobecodebanc): d
Checking position: 2 of String (adobecodebanc): o
Checking position: 3 of String (adobecodebanc): b
Checking position: 4 of String (adobecodebanc): e
Checking position: 5 of String (adobecodebanc): c
Checking position: 6 of String (adobecodebanc): o
Checking position: 7 of String (adobecodebanc): d
Checking position: 8 of String (adobecodebanc): e
Checking position: 9 of String (adobecodebanc): b
Checking position: 10 of String (adobecodebanc): a
Checking position: 11 of String (adobecodebanc): n
Checking position: 12 of String (adobecodebanc): c
Additional access to listS via index 0:
a
End
Another thing is that you don't need to use indexes to iterate your string characters (or break your string into an array of characters with list()). You can just shortcut with this:
for i in nuS:
print i
While I'm not sure this counts as an answer, I agree that the provided code runs without issue.
– Windmill
Nov 29 '18 at 2:09
Thanks for your help. I turned the argument of the function into a string and then into a list just to ensure I was dealing with a list and not with a unicode or string. Although when i tried printing the value of s directly (s[0]) it also gave me the same error
– Kbeher
Nov 29 '18 at 8:10
add a comment |
You probably are running into a strange issue with your provider python compiler or maybe, since it's a page with a web application to test, run and validate python exercices, your code is not running exactly the way you see it on the web page.
This can be clearly spoted if you compare your code to the stacktrace, which has additional calls and even the arguments for your function don't seem to be standard formal arguments, but a *args (non-keyworded variable length argument) argument.
A stacktrace for an IndexError for your code (for example if your string argument was empty, would be):
Traceback (most recent call last):
File "main.py", line 11, in <module>
minSubstringWithAllChars("abe", "")
File "main.py", line 9, in minSubstringWithAllChars
print listS[0]
IndexError: list index out of range
Anyway, running With python 2.7.13 clearly shows no problem with your code:
def minSubstringWithAllChars(s):
nuS = str(s)
listS = list(nuS)
print("working with string: " + nuS)
print("Len of s: " + str(len(nuS)))
print("Len of listS: " + str(len(listS)))
print("n")
for i in range(len(listS)):
print("Checking position: " + str(i) + " of String (" + nuS + "): " + listS[i])
print("nAdditional access to listS via index 0: ")
print listS[0]
print("Startn")
minSubstringWithAllChars("adobecodebanc")
print("nEnd")
You can see it works:
Start
working with string: adobecodebanc
Len of s: 13
Len of listS: 13
Checking position: 0 of String (adobecodebanc): a
Checking position: 1 of String (adobecodebanc): d
Checking position: 2 of String (adobecodebanc): o
Checking position: 3 of String (adobecodebanc): b
Checking position: 4 of String (adobecodebanc): e
Checking position: 5 of String (adobecodebanc): c
Checking position: 6 of String (adobecodebanc): o
Checking position: 7 of String (adobecodebanc): d
Checking position: 8 of String (adobecodebanc): e
Checking position: 9 of String (adobecodebanc): b
Checking position: 10 of String (adobecodebanc): a
Checking position: 11 of String (adobecodebanc): n
Checking position: 12 of String (adobecodebanc): c
Additional access to listS via index 0:
a
End
Another thing is that you don't need to use indexes to iterate your string characters (or break your string into an array of characters with list()). You can just shortcut with this:
for i in nuS:
print i
You probably are running into a strange issue with your provider python compiler or maybe, since it's a page with a web application to test, run and validate python exercices, your code is not running exactly the way you see it on the web page.
This can be clearly spoted if you compare your code to the stacktrace, which has additional calls and even the arguments for your function don't seem to be standard formal arguments, but a *args (non-keyworded variable length argument) argument.
A stacktrace for an IndexError for your code (for example if your string argument was empty, would be):
Traceback (most recent call last):
File "main.py", line 11, in <module>
minSubstringWithAllChars("abe", "")
File "main.py", line 9, in minSubstringWithAllChars
print listS[0]
IndexError: list index out of range
Anyway, running With python 2.7.13 clearly shows no problem with your code:
def minSubstringWithAllChars(s):
nuS = str(s)
listS = list(nuS)
print("working with string: " + nuS)
print("Len of s: " + str(len(nuS)))
print("Len of listS: " + str(len(listS)))
print("n")
for i in range(len(listS)):
print("Checking position: " + str(i) + " of String (" + nuS + "): " + listS[i])
print("nAdditional access to listS via index 0: ")
print listS[0]
print("Startn")
minSubstringWithAllChars("adobecodebanc")
print("nEnd")
You can see it works:
Start
working with string: adobecodebanc
Len of s: 13
Len of listS: 13
Checking position: 0 of String (adobecodebanc): a
Checking position: 1 of String (adobecodebanc): d
Checking position: 2 of String (adobecodebanc): o
Checking position: 3 of String (adobecodebanc): b
Checking position: 4 of String (adobecodebanc): e
Checking position: 5 of String (adobecodebanc): c
Checking position: 6 of String (adobecodebanc): o
Checking position: 7 of String (adobecodebanc): d
Checking position: 8 of String (adobecodebanc): e
Checking position: 9 of String (adobecodebanc): b
Checking position: 10 of String (adobecodebanc): a
Checking position: 11 of String (adobecodebanc): n
Checking position: 12 of String (adobecodebanc): c
Additional access to listS via index 0:
a
End
Another thing is that you don't need to use indexes to iterate your string characters (or break your string into an array of characters with list()). You can just shortcut with this:
for i in nuS:
print i
edited Nov 29 '18 at 10:40
answered Nov 29 '18 at 2:06
Alexandre JumaAlexandre Juma
542315
542315
While I'm not sure this counts as an answer, I agree that the provided code runs without issue.
– Windmill
Nov 29 '18 at 2:09
Thanks for your help. I turned the argument of the function into a string and then into a list just to ensure I was dealing with a list and not with a unicode or string. Although when i tried printing the value of s directly (s[0]) it also gave me the same error
– Kbeher
Nov 29 '18 at 8:10
add a comment |
While I'm not sure this counts as an answer, I agree that the provided code runs without issue.
– Windmill
Nov 29 '18 at 2:09
Thanks for your help. I turned the argument of the function into a string and then into a list just to ensure I was dealing with a list and not with a unicode or string. Although when i tried printing the value of s directly (s[0]) it also gave me the same error
– Kbeher
Nov 29 '18 at 8:10
While I'm not sure this counts as an answer, I agree that the provided code runs without issue.
– Windmill
Nov 29 '18 at 2:09
While I'm not sure this counts as an answer, I agree that the provided code runs without issue.
– Windmill
Nov 29 '18 at 2:09
Thanks for your help. I turned the argument of the function into a string and then into a list just to ensure I was dealing with a list and not with a unicode or string. Although when i tried printing the value of s directly (s[0]) it also gave me the same error
– Kbeher
Nov 29 '18 at 8:10
Thanks for your help. I turned the argument of the function into a string and then into a list just to ensure I was dealing with a list and not with a unicode or string. Although when i tried printing the value of s directly (s[0]) it also gave me the same error
– Kbeher
Nov 29 '18 at 8:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53528909%2fwhy-am-i-getting-a-list-index-error-when-there-are-certainly-items-in-the-list-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you please include the text of your error message (stack trace)?
– MyStackRunnethOver
Nov 28 '18 at 22:44
No problem. I added it at the bottom of my question
– Kbeher
Nov 29 '18 at 1:26
Instead of printing listS[0], try to
print(listS)
just to see how the list looks like before you try and access it via index.– Alexandre Juma
Nov 29 '18 at 2:12
It looks just like the list you would expect to see. Here is the what I printed out when i printed the entire list. ['a', 'd', 'o', 'b', 'e', 'c', 'o', 'd', 'e', 'b', 'a', 'n', 'c']. This shows that there is an item in the index 0 of listS. So i'm not sure what's going on!
– Kbeher
Nov 29 '18 at 8:06
I think it's the platform you're using. If you see the stacktrace, it's clearly not only executing what you've shown here. There are other intermediate calls and even the arguments to minSubstringWithAllChars are passed as non-keyworded variable length argument (*_fArgs_nycavblqytqu) and not formal args we're testing here. Also a stacktrace of a Indexerror with that code would produce this stacktrace:
File "main.py", line 11, in <module> minSubstringWithAllChars("abe", "") File "main.py", line 9, in minSubstringWithAllChars print listS[7] IndexError: list index out of range
– Alexandre Juma
Nov 29 '18 at 10:20