Skip to content

Python add two lists element wise

  • by

Adding two lists element-wise in Python means performing addition on corresponding elements from two lists to create a new list with the results.

For example, given two lists list1 and list2:

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

Element-wise addition would mean adding the first element of list1 to the first element of list2, the second element of list1 to the second element of list2, and so on, resulting in a new list containing the sums:

result_list = [1 + 5, 2 + 6, 3 + 7, 4 + 8]

Therefore, result_list would be [6, 8, 10, 12]. The operation combines corresponding elements from both lists and creates a new list containing the sums of those elements. This is known as element-wise addition.

Python adds two lists element-wise example

To add two lists element-wise in Python, you can use a simple loop or utilize list comprehensions. Here are both approaches:

Method 1: Using a loop

def add_lists_elementwise(list1, list2):
    if len(list1) != len(list2):
        raise ValueError("Lists must be of the same length")
    
    result = []
    for i in range(len(list1)):
        result.append(list1[i] + list2[i])
    
    return result

# Example usage:
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
result_list = add_lists_elementwise(list1, list2)
print(result_list)  # Output: [6, 8, 10, 12]

Method 2: Using list comprehensions

def add_lists_elementwise(list1, list2):
    if len(list1) != len(list2):
        raise ValueError("Lists must be of the same length")
    
    return [x + y for x, y in zip(list1, list2)]

# Example usage:
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
result_list = add_lists_elementwise(list1, list2)
print(result_list)  # Output: [6, 8, 10, 12]

Both methods will add the corresponding elements of the two lists together, provided that the lists have the same length. If the lists have different lengths, the ValueError will be raised to indicate that the operation is not possible.

Or you can use the following code:

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

# Using list comprehensions
result_list = [x + y for x, y in zip(list1, list2)]
print(result_list)  # Output: [6, 8, 10, 12]

In this example, the zip() function is used to pair up corresponding elements from list1 and list2. The list comprehension then iterates through these pairs and adds the elements together, creating a new list containing the result of the element-wise addition.

Keep in mind that for this method to work correctly, both lists should have the same length. If the lists have different lengths, zip() will stop pairing elements when the shortest list ends and any extra elements in the longer list will be ignored.

You can create a function for it

def add_lists_elementwise(list1, list2):
    if len(list1) != len(list2):
        raise ValueError("Lists must be of the same length")
    
    result = []
    for i in range(len(list1)):
        result.append(list1[i] + list2[i])
    
    return result

# Example usage:
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

result_list = add_lists_elementwise(list1, list2)
print(result_list) 

Output:

Python add two lists element wise

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *