You can use either of the following templates in order to concatenate two lists in Python:
(1) Using the + operator:
list_one = ['item1','item2','item3',....] list_two = ['item1','item2','item3',....] concatenated_list = list_one + list_two print (concatenated_list)
(2) Using extend:
list_one = ['item1','item2','item3',....] list_two = ['item1','item2','item3',....] list_one.extend(list_two) print (list_one)
In the next section, you’ll see how to apply the above techniques in practice.
Steps to Concatenate Two Lists in Python
Step 1: Create two Lists
To begin with a simple example, let’s create two simple lists that contain string values about fruits:
list_one = ['Banana','Apple','Mango','Watermelon','Pear'] list_two = ['Blueberry','Cherry','Pineapple','Papaya','Coconut'] print (list_one) print (list_two)
Run the code in Python, and you’ll get these two lists:
Step 2: Concatenate the two Python Lists using the + operator
You can use the + operator in order to concatenate the two lists:
list_one = ['item1','item2','item3',....] list_two = ['item1','item2','item3',....] concatenated_list = list_one + list_two print (concatenated_list)
For our example:
list_one = ['Banana','Apple','Mango','Watermelon','Pear'] list_two = ['Blueberry','Cherry','Pineapple','Papaya','Coconut'] concatenated_list = list_one + list_two print (concatenated_list)
As you can see, the two lists are now concatenated:
Similarly, you can use the + operator to concatenate two lists that contain integers:
list_one = [1,2,3,4,5] list_two = [6,7,8,9,10] concatenated_list = list_one + list_two print (concatenated_list)
Here is the result:
Using Extend
Alternatively, you can use extend to concatenate the two lists:
list_one = ['item1','item2','item3',....] list_two = ['item1','item2','item3',....] list_one.extend(list_two) print (list_one)
Here is the complete Python code for our example:
list_one = ['Banana','Apple','Mango','Watermelon','Pear'] list_two = ['Blueberry','Cherry','Pineapple','Papaya','Coconut'] list_one.extend(list_two) print (list_one)
As before, the two lists are now concatenated:
Concatenate More Than Two Lists
You can use the + operator to concatenate multiple lists.
For example, let’s concatenate the following 4 lists:
list_one = ['Banana','Apple','Mango'] list_two = ['Watermelon','Pear'] list_three = ['Blueberry','Cherry'] list_four = ['Pineapple','Papaya','Coconut'] concatenated_list = list_one + list_two + list_three + list_four print (concatenated_list)
Run the code, and you’ll get the following result:
Finally, you can check the following guide to learn more about creating a list in Python.