Home > Software engineering >  two sum leetcode question in python using list comprehension
two sum leetcode question in python using list comprehension

Time:03-29

i am solving "two sum" problem of leetcode using only list comprehension in python3. please tell my mistakes in my code :

nums=[[i,j] for i in range(len(nums)) for j in range(i,len(nums)) if nums[i] nums[j]==target]

but it is giving error

CodePudding user response:

The nums in your code will be list[list] like [[0, 1]]. So what they want is list like [0, 1]

nums = [2, 7, 11, 15]
target = 9
nums = [[i,j] for i in range(len(nums)) for j in range(i, len(nums)) if nums[i] nums[j] == target and i != j][0]
print(nums)
  • Related