Add Two Lists in Python: Step-by-Step Guide
You can use the zip function to add up two lists. For example:
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
sum_list = [x + y for x, y in zip(list1, list2)]
print(sum_list) # 输出 [7, 9, 11, 13, 15]
In this example, the zip function extracts elements at corresponding positions from two lists, then uses list comprehension to add the elements at corresponding positions and stores the result in a new list called sum_list.