Finding Pair Sums in a List

  • Share this:

Code introduction


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


Technology Stack : list, set, iterator, return value

Code Type : Function

Code Difficulty : Intermediate


                
                    
def sum_pairs(numbers, target):
    seen = set()
    for number in numbers:
        complement = target - number
        if complement in seen:
            return [complement, number]
        seen.add(number)
    return None