Your code contains a few lines that accomplish nothing and obfuscate your intent:
else: continue
If the conditional is false, you'll automatically
continue
on to the next iteration without having to tell the program to do that.return None
All Python functions implicitly
return None
; however,. While PEP 8 recommendsappears to endorse this practice ("explicit is better than implicit"), it seems noisy to me.num_lst = list(range(len(nums)))
effectively generates a list of all the indices in thenums
input list. Then, you immediatelyenumerate
this list, which produces pairs of identical indicesindx, num
. If all you're attempting to do is iterate, this is significant obfuscation; simply callenumerate
directly onnums
to produce index-element tuples:def twoSum(self, nums, target): for i, num in enumerate(nums): for j in range(i + 1, len(nums)): if num + nums[j] == target: return [i, j]
This makes the intent much clearer: there are no duplicate variables with different names representing the same thing. It also saves unnecessary space and overhead associated with creating a list from a range.
Following on the previous item,
indx, num
andnum_lst
are confusing variable names, especially when they're all actually indices (which are technically numbers).