A One-Pass Hash Table Solution to twoSum I read the guiding solution to twoSum in leetcodes ;  


  Given an array of integers, return indices of the two numbers such that they add up to a specific target.
  
  You may assume that each input would have exactly one solution, and you may not use the same element twice.
  
  Example:

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

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

  
  Approach 3: One-pass Hash Table
  It turns out we can do it in one-pass. While we iterate and inserting elements into the table, we also look back to check if current element's complement already exists in the table. If it exists, we have found a solution and return immediately.


and mimic a python solution 

class Solution:
    def twoSum(self, nums, target) -&gt; List[int]:
        """
        :type nums: List[int]
        :type target: int
        """
        nums_d = {}

        for i in range(len(nums)):
            complement = target - nums[i]

            if nums_d.get(complement) != None: #Check None not True Value
                return [i, nums_d.get(complement)]         
            nums_d[nums[i]] = i    #produce a map  
        return [] 


Unfortunately, my solution is only faster than 81%, which I thought was a best solution.


  Runtime: 40 ms, faster than 81.00% of Python3 online submissions for Two Sum.
  Memory Usage: 14.3 MB, less than 5.08% of Python3 online submissions for Two Sum.
  Next challenges:


How could continue to improve the code, and I am curious about the approaches of the top 20%.
Don't leave white space at the end of lines.
Use enumerate.
When comparing with None use is and is not.
It'd be cleaner to just use in rather than get.
If you want to use nums_d.get then you should use it outside the if so you don't have to use it a second time when you enter the if.
This however makes the code messy for not much of a benefit IMO.
Unless the site forces you to return lists returning a tuple would be more Pythonic.
Your comments aren't helpful, if anything they make the code harder to read for me.
The variable name nums is easier to read then nums_d, the _d is useless.
When returning it would be better to either:


Raise an exception, as the lookup failed.
Return a tuple where both values are None. This is so you can tuple unpack without errors.



Getting the code:

def test_peil(nums: List[int], target: int) -&gt; Tuple[int, ...]:
    lookup = {}
    for i, v in enumerate(nums):
        if target - v in lookup:
            return i, lookup[target - v]
        lookup[v] = i
    raise ValueError('Two sums target not in list.')


You can further improve performance by including Alain T.'s change for small numbers.

def test_peil_alain(nums: List[int], target: int) -&gt; Tuple[int, ...]:
    if len(nums) &lt;= 100:
        for i, n in enumerate(nums):
            if target - n in nums:
                return i, nums.index(target - n)
    else:
        lookup = {}
        for i, v in enumerate(nums):
            if target - v in lookup:
                return i, lookup[target - v]
            lookup[v] = i
    raise ValueError('Two sums target not in list.')


And has a performance improvement: (the graph is volatile due to only using one input sample)