Finding the index of a number in a list in python

Solution 1:

def twoSum(nums, target):
    for i, x in enumerate(nums):
        for j, z in enumerate(nums):
            if i == j:
                continue
            res = x + z
            if res == target:
                result = [i, j]
                return result
  • You can use enumerate to access the indexes.
  • You can use continue instead of pass, but it's just a preference.
  • If you use continue you don't need the else since the execution is gonna jump to the next iteration when continue is hit.