In this tutorial, we are going to discuss some methods which are used to append one list to another in Python. It covers two main problems which are listed below along with examples.
Problem 1: Append one list to another list For Example: Input List 1 = [63, 52, 21] Input List 2 = [61, 76, 92, 43] Output=[63, 52, 21, 61, 76, 92, 43] Problem 2: Append one list as an element into the another list For Example: Input List 1 = [63, 52, 21] Input List 2 = [61, 76, 92, 43] Output=[63, 52, 21, [61, 76, 92, 43]]
Some of the common ways to append one list to another in Python are:
- Append one list to another using list.extend.
- Appending one list to another using list.append.
- Append using the chain function in itertools modules.
- Append multiple lists using for loop.
- Append using concatenation + operator.
Using list.extend() to Combine two lists
Using the extend technique, you may create two lists and then merge the second list into the primary list. The following code example adds list2 to list1 using the extend method. In this way, you can append one list to another in Python using list.extend() method.
// Python code method 1
list1 = [5, 6, 8]
list2 = [45, 5, 7]
list1.extend(list2)
print(list1)
// code ends here
Output:
[5, 6, 8, 45, 5, 7]
With the extend method, you can easily add a new list to an existing list by a simple function.
Using list.append to append one list to another in python
This is another method to merge or combine two lists using a simple built-in python function.
// Python code method 2
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_1.append(list_2)
print(list1)
// code ends here
Output:
[1, 2, 3, 4, [5, 6, 7, 8]]
Using the chain function in the itertools module
This function chain()
in the itertools module takes a variable num of the same type iterables and concatenates them together in sequence based on the parameters. Chain() function can be used for appending two or more lists into a single list.
// Python code method 3
import itertools
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]
list_all = list(itertools.chain(list_1, list_2, list_3))
print(list_all)
// code ends here
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
using for loop for Appending a list to another list
We can also use a For Loop to iterate over the elements of the second list and append each of these elements to the first list using the list.append() function.
// Python code method 4
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
for item in list_2:
list_1.append(item)
print(list1)
// code ends here
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
Using the Concatenation + Operator for appending
Adding several lists together with the + operator in Python is also a common way to do so. Using the + operator is one of the simplest methods and it is the most widely used method also.
// Python code method 5
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]
list_all = list_1 + list_2 + list_3
print(list_all)
// code ends here
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
In order to keep it brief, Extending, chaining, or utilizing the concatenation + operator are all straightforward and efficient techniques to add extra lists to the main list. What truly matters is convenience and personal preference, as all three solutions perform well.