Finding Target-Sum Pairs in a List

  • Share this:

Code introduction


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


Technology Stack : Built-in data types (list, set)

Code Type : Function

Code Difficulty : Beginner


                
                    
def sum_pairs(numbers, target):
    seen = set()
    for num in numbers:
        diff = target - num
        if diff in seen:
            return (diff, num)
        seen.add(num)
    return None