Reputation: 1731
Not sure if this is BECAUSE of Jupyter Notebook or just that Python HANDLES the exception, but I was solving a problem involving algorithms with a friend
While whiteboarding the code, I used Python!
exampleList = ['a', 'b', 'c', 'd', 'e']
for i in range(0, len(exampleList)):
for j in range(i + 1, len(exampleList)):
print(exampleList[i], exampleList[j])
Basically, the debate was that.. This code (a similar one, I have used a simple example to illustrate) involving two for
loops MUST throw an error! Because, index i
reached len(exampleList) - 1
, and index j
technically will become len(exampleList)
and at that point, print(exampleList[i], exampleList[j])
cannot work! It looks like while running the code, it does print out perfectly, and I think HANDLES the IndexError: list index out of range
exception!
My question is.. Is this supposed to be intended behaviour in Python? Because the person I was debating with ended up telling me that 'The people conducting your interview notice these things! You must be able to explain every line.'
I would like to understand how this part is handled so I can explain why this does not throw an error!
Upvotes: 1
Views: 1444
Reputation: 582
This is no mistake on Python's part. The exception only becomes a worry when trying to access exampleList[5]
, which is never requested by the loop. It is important to remember that range()
is exclusive of the upper limit, but inclusive of the lower.
Below I added some comments to the right to explain
exampleList = ['a', 'b', 'c', 'd', 'e']
for i in range(0, len(exampleList)): # includes [0, 1, 2, 3, 4]
for j in range(i + 1, len(exampleList)): # includes all numbers in range[0,4] greater than i
print(exampleList[i], exampleList[j]) # will never exceed 4
Upvotes: 0
Reputation: 1000
In the last iteration of the outermost loop, j will start equal to len(example). Therefore, the j-loop is not even executed in this iteration. Because the range is between len(example) and len(example) - 1.
Upvotes: 0
Reputation: 377
For this case, we have len(array) = 5.
The range function on the first loop will return 5 values iteratively - [0, 1, 2, 3, 4]. That means that the issue number should be the last, 4, right?
But actually, when you attempt to enter the second loop, range(4 + 1, 5) will essentially return a blank list of numbers, therefore it won't run the contents of the loop at all. Hence, no error.
Upvotes: 1
Reputation: 2947
In your sample code, j
never equals len(exampleList)
. You say j in range(i + 1, len(exampleList))
, which means j
will also always be less than len(exampleList)
. And, if i+1 >= len(exampleList)
, the range will be empty because range(m, n)
where m >= n
is always empty.
So the error is never even thrown because erroneous code is never executed.
Upvotes: 1
Reputation: 4776
The range
function is inclusive of lower bound, but exclusive of upper bound.
Your example list has 5 elements. This means that the first loop is running range(0,5)
, making the max i=4
. Which makes the second loop have a max value of range(5,5)
, which is an empty list. Iterating over an empty list causes 0 iterations.
>>> exampleList = ['a', 'b', 'c', 'd', 'e']
>>> len(exampleList)
5
>>> range(0, 5)
[0, 1, 2, 3, 4]
>>> range(5,5)
[]
Upvotes: 1
Reputation: 146
Notice how exampleList[i] is never e? Once your first for loop hits len(exampleList) - 1, the inner loop fails because it is checking if i + 1 is less than the length of exampleList (which it isn't), therefore the print statement is never reached causing the exception to not be thrown.
Upvotes: 0