Is there a problem with python nesting two for loops?

title requirements:

given nums = [2,7,11,15], target = 9

because nums [0] + nums [1] = 2 + 7 = 9
, return index [0,1]

topic Source: sum Sum on leetcode

personal code

def twoSum(nums, target):
    l = []  -sharp 
    s = 0  -sharp 
    for _ in nums:
        i = 0
        s = s + 1
        p = nums.pop(0)
        l.append(p)
        for v in nums:
            if v == target - p:
                return [len(l) - 1, s + i]
            i = i + 1

personal thoughts and problems

my idea is to take an element into the external stack, record its value, and then traverse the elements that meet the criteria in the rest of the list.
s , I , in order to mark the sequence function of the elements that meet the requirements.
validation process found that every time I type

print(twoSum([1,3,3,5,2],7))

debugging results will show None , that is, when both numbers are at the end of the input list , None .

python novice, please give me more advice.

Oct.06,2021

come to the conclusion that the problem lies in the simultaneous use of for _ in nums: and nums.pop (0) . This will cause you to take a value for each iteration from a shrinking list. You can debugger or show _ of each loop to print , and you will find that you have jumped once in each loop.

incidentally, a solution: with the same complexity n ^ 2 but easy to understand

is given.
def twoSum(nums, target):
    for i in range(len(nums)):
        j = i + 1
        tmp = nums[i] + nums[j]
        if tmp == target:
            return [i, j]

print(twoSum([1,4,3,5,2],7))
Menu