This article tells us how to append one list to another in Python. It covers two main problems which are listed below along with examples. If you want to learn more about lists in Python Programming, visit Python Programming Lists.
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]]
Method 1: 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.
Method 2: Using list.append to append one list to another in python
This is another method to merge or combine two lists using 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]]
method 3: Using the chain function in the itertools module
This function chain()
in the itertools module takes a variable num of same type iterables and concatenates them together in sequence based on the parameters. Chain() function can be used append 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]
Method 4: using for loop to Append a list to another list
We can also use a For Loop to iterate over the elements of second list, and append each of these elements to the first list using 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]
Method 5: Using the Concatenation + Operator
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 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, the Extending, chaining, or utilizing the concatenation + operator are all straightforward and efficient techniques to add extra lists to a main list. What truly matters is convenience and personal preference, as all three solutions perform well.