Use for loop to report error IndexError: list index out of range

topic description

remove duplicates from the sorted array

sources of topics and their own ideas

I use this code to remove duplicates from the sorted array, which is the algorithm problem on LeetCode. I first use the for loop to write, and I feel that the train of thought is fine, but I always reported an error IndexError: list index out of range, and then checked the answer. I just changed for to while, and then passed the check. Is this because the for loop range () function only evaluates len (nums), on the first call and the value of range does not change.

related codes

/ / Please paste the code text below (do not replace the code with pictures)
this is the for loop:
1 nums = 1]
2 count = 0
3 if len (nums) = = 1 or len (nums) = = 0:
4 count = len (nums)
5 else:
6 for i in range (len (nums)-1):
7 print (len (nums))
8 if nums [I] = nums [iTunes 1]:
9 del nums
10 else:
11 count + = 1
12
13 print (count)
error: IndexError: list index out of range
this is the correct version of while:
class Solution:

def removeDuplicates(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    i = 0
    if len(nums) == 1 or len(nums) == 0:
        return len(nums)
    else:
        while i < (len(nums)-1):  
            if nums[i] == nums[i+1]:
                del nums[i+1]
            else:
                i += 1     
        return 


what result do you expect? What is the error message actually seen?

there is also a small problem, my while code, finally only wrote return can return the correct result, what is the reason? Is it because of LeetCode that he made it up for me? Novice, only learned the basic grammar, some places are not clear, thank you!

Jun.08,2021

the answer to your first question is: yes, he will first calculate the value of range (len (nums)-1) , and then loop, and each time while executes, it will determine whether the condition is True

.

second question: maybe you don't have the foundation of C language. The emergence of return has two meanings: (1). Returns the value to be returned, (2). Represents the end of the function, but this problem does not need to return a value, and the function is all executed in the function body. So can also be

without adding return.

third question: do you know why no one has answered you for so long? Some of your code does not have Markdown syntax, it looks like is very messy , programmers is the most annoying this, secondly, the text expression is too long , people really don't want to see it. All right, that's it for you

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3b8d9-2c2b9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3b8d9-2c2b9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?