Join Two Lists
Python gives you several ways to combine, or concatenate, two or more lists.
The + operator is among the simplest.
Example
Combine two lists with +:
Prompt
Write a Python program that creates two lists: list1 with "a", "b", "c" and list2 with 1, 2, 3.
Join the two lists using the + operator and print the result.
Output: complete Python program
main.py
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
Example
Add list2 to list1 item by item:
Prompt
Write a Python program that creates two lists: list1 with "a", "b", "c" and list2 with 1, 2, 3.
Append all items from list2 into list1 one by one using a for loop and print list1.
Output: complete Python program
main.py
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
Example
Let extend() add list2 to the end of list1:
Prompt
Write a Python program that creates two lists: list1 with "a", "b", "c" and list2 with 1, 2, 3.
Use the extend() method to add list2 at the end of list1 and print list1.
Output: complete Python program
main.py
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)