Sum Corresponding Elements of Two Lists

  • Share this:

Code introduction


The function takes two lists as arguments and calculates the sum of corresponding elements in both lists, returning the resulting list. If the two lists are not of the same length or not lists, an exception is raised.


Technology Stack : Python built-in functions, list comprehension, zip

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
import random

def random_list_sum(arg1, arg2):
    if not isinstance(arg1, list) or not isinstance(arg2, list):
        raise ValueError("Both arguments must be lists")
    
    if len(arg1) != len(arg2):
        raise ValueError("Both lists must have the same length")
    
    return sum(a + b for a, b in zip(arg1, arg2))