Finding Target-Sum Pairs in a List

  • Share this:

Code introduction


This function takes a list of numbers and a target value, and it returns a pair of numbers that sum up to the target value. If no such pair is found, it returns None.


Technology Stack : List (list), Dictionary (dict)

Code Type : Function

Code Difficulty : Beginner


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