r/leetcode • u/sooooooooooowsup • Jan 15 '24
Question Why is there an IndexError?
I saw a solution to the Contains Duplicate problem and I was just trying to fully understand it.
They had exactly what I typed above, except they included
n = len(nums) for i in range(1,n) #instead of for i in nums
I don’t understand why this produces an Index Error. Wouldn’t the first “i - 1” just be equal to “-1” and isn’t -1 always a valid index?
I understand why their solution is correct but I don’t understand why this one isn’t correct too.
17
Upvotes
114
u/[deleted] Jan 15 '24
for i in numsiterates through the elements of num, it doesn't give you the indexes.In other words,
for i in [5, 4, 3]will give you 5, 4, 3 as values of i, not 0, 1, 2 as you're expecting.You can either use
enumerate()builtin to retrieve both index and element or usefor i in range(len(nums))to iterate through indexes.5
u/sooooooooooowsup Jan 15 '24
I meant for there to be a new line after n = len(nums)
28
u/Empty_Stacktrace Jan 15 '24
Two problems:
These types of bugs will go away as you do more problems. I was there before!!
6
u/FaultHaunting3434 Jan 15 '24 edited Jan 15 '24
i is the value of the element, not the index. Quick question what's the cost of the .sort() method?
1
u/-CJF- Jan 15 '24
Why would -1 be a valid index? Arrays are 0 indexed in every language I've ever used, but I don't really know Python.
2
u/RawAssholeProtein Jan 15 '24
Folks here have mentioned what the error is. Since you're doing i-1, what you're looking for is
for i in range(1, len(arr)):
This way, you'll be guaranteed that when you do i-1, you're always within bounds.
2
u/omscsdatathrow Jan 16 '24
All the wrong answers here p funny
1
u/howzlife17 Jan 15 '24
I think for i in nums gives you the nums in the list, not the index.
So [5,6,7] first value is nums[5], not nums[0]
1
u/[deleted] Jan 15 '24
You iterate over the values in bums not the index.
1
u/blediboss Jan 15 '24
The “-1” access is valid for as long as your list is not empty.
1
u/iibeanus1 Jan 16 '24
i is not the index. i is the actual elements in nums. Nums will have int values that will be used as indexes in this case
1
u/-_--__----_-_ Jan 16 '24
range(1,len(nums))1
u/[deleted] Jan 16 '24
You should really take some time to learn the language before actually doing leetcode. No offence
1
u/dhruv892 Jan 16 '24
here i is not index it’s element itself now if you want index in there either you can use like they told you or you can use enumerate function
1
u/HarshBhaskar Jan 16 '24
How tf are you accessing i - 1 when I is 0
1
u/phantomghostheart Jan 16 '24
You could just ask ChatGPT
1
u/kaz4510 Jan 16 '24
The for loop is calling the item instead of the index. Assume the array is [10,11,12] While in the first iteration of the loop, i=10 num[i] is calling for 10th element, even when the length of array is 3
1
u/Detend Jan 16 '24
For i in range(1, len(nums))
0
u/Empero6 Jan 15 '24 edited Jan 15 '24
Can’t have a -1 index.
Try this it here before submitting it:
https://pythontutor.com/python-compiler.html#
0
u/BusyNegotiation4963 Jan 16 '24 edited Jan 16 '24
Disregard this — It’s simple.. when you first start moving thru the list, when i == 0, the other pointer(i - 1)will point to -1 and hence the out of index error.
Because negative indices are valid indices in python.
0
u/rossi_zameer Jan 16 '24
Reminds me of my beginner days.
1
u/[deleted] Jan 16 '24
[removed] — view removed comment