-1

ValueError: 0 is not in list

I want to print the indices of nums[x] and nums[y] from the following code. But I am getting an error, ValueError: 0 is not in list. Please, help me out. Thanks.

def twoSum(nums = [], target = 0):
  if len(nums) > 0:
    for x in range(len(nums)):
      for y in range(len(nums)):
        l = nums[x] + nums[y]
      if l == target:
        print(nums.index(x), nums.index(y))

twoSum(nums = [2,6,5], target = 7)
  • 2
    Don't use an [empty list as a default parameter](https://stackoverflow.com/questions/366422/what-is-the-pythonic-way-to-avoid-default-parameters-that-are-empty-lists). – Chillie Jan 26 '22 at 14:13

1 Answers1

2
print(nums[x], nums[y])

or if you want to get index then:

print(x, y)

You considering each number of list twice(Which is wrong)

for x in range(len(nums)-1):
      for y in range(1,len(nums)):
Mazhar
  • 1,044
  • 6
  • 11