how to add 2 lists together code example

Example 1: calculate the addition of two lists in python

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

# `zipped_lists` contains pairs of items from both lists.
# Create a list with the sum of each pair.
sum = [x + y for (x, y) in zip(list1, list2)] 

print(sum)
# [5, 7, 9]

Example 2: python add elements of two lists together

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

for (item1, item2) in zip(list1, list2):
	sum_list.append(item1 + item2)

print(sum_list)

Example 3: merge two lists

# Makes list1 longer by appending the elements of list2 at the end.
list1.extend(list2)

Example 4: how to concatenate two lists in python

listone = [1,2,3]
listtwo = [4,5,6]

joinedlist = listone + listtwo

Tags:

Misc Example