Finding Target-Sum Pairs in a List

  • Share this:

Code introduction


The function finds a pair of numbers in a list that sum up to a given target value. If no such pair is found, it returns None.


Technology Stack : List, Dictionary

Code Type : Function

Code Difficulty : Beginner


                
                    
def a_sum_pairs(numbers, target):
    seen = {}
    for number in numbers:
        complement = target - number
        if complement in seen:
            return (complement, number)
        seen[number] = True
    return None