How to add two lists in Python
In Python, adding two lists together involves concatenating them, which means combining the elements of both lists into a single list. This does not perform arithmetic addition on the elements; it simply joins the lists end-to-end.
1. Concatenating Two Lists
Concatenation combines all elements of two lists into one new list. This is done using the + operator.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# Concatenating the lists
result = list1 + list2
# Output
print("List 1:", list1) # Original List 1
print("List 2:", list2) # Original List 2
print("Concatenated List:", result) # Combined list
Output:
List 1: [1, 2, 3]
List 2: [4, 5, 6]
Concatenated List: [1, 2, 3, 4, 5, 6]
How it works:
+creates a new list by appending all elements oflist2tolist1.- The original lists remain unchanged.
2. Element-Wise Addition
Element-wise addition adds corresponding elements of two lists. This is useful when both lists contain numbers, and you want a new list of their sums.
Using a For Loop
In this method, we loop through the indices of the lists and add elements at the same index.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# Element-wise addition using a loop
result = []
for i in range(len(list1)):
result.append(list1[i] + list2[i])
# Output
print("List 1:", list1)
print("List 2:", list2)
print("Element-Wise Sum:", result)
Output:
List 1: [1, 2, 3]
List 2: [4, 5, 6]
Element-Wise Sum: [5, 7, 9]
Using List Comprehension
This method is a shorter way to perform the same operation as above.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# Element-wise addition using list comprehension
result = [list1[i] + list2[i] for i in range(len(list1))]
# Output
print("List 1:", list1)
print("List 2:", list2)
print("Element-Wise Sum:", result)
Output:
List 1: [1, 2, 3]
List 2: [4, 5, 6]
Element-Wise Sum: [5, 7, 9]
Using zip Function
The zip function pairs elements from two lists together for processing.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# Element-wise addition using zip
result = [x + y for x, y in zip(list1, list2)]
# Output
print("List 1:", list1)
print("List 2:", list2)
print("Element-Wise Sum:", result)
Output:
List 1: [1, 2, 3]
List 2: [4, 5, 6]
Element-Wise Sum: [5, 7, 9]
zip Explanation:
- Combines elements from both lists into pairs.
zip(list1, list2)produces[(1, 4), (2, 5), (3, 6)].- The list comprehension
[x + y for x, y in zip(list1, list2)]adds each pair.
3. Using NumPy for Element-Wise Addition
The NumPy library is optimized for numerical operations and can handle element-wise operations easily.
Output:
List 1: [1, 2, 3]
List 2: [4, 5, 6]
Numpy Array Result: [5 7 9]
Converted Back to List: [5, 7, 9]
4. Extending a List (In-Place Addition)
The extend method modifies one list by adding all elements of another list to it. This operation is done in place, meaning the original list is changed.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# Extend list1 with elements of list2
list1.extend(list2)
# Output
print("Modified List 1:", list1) # Modified in place
print("List 2 (Unchanged):", list2)
Output:
Modified List 1: [1, 2, 3, 4, 5, 6]
List 2 (Unchanged): [4, 5, 6]
Key Takeaways:
- Concatenation (
+) creates a new list with elements of both lists. - Element-wise addition creates a new list in which each element is the sum of elements at the same index in the two lists.
zipis a very helpful function to match elements.- NumPy is very effective for numerical calculations.
extendmodifies the list in place.