Python possesses a wide range of in-built data structures, which helps programmers to code effectively and quickly. Lists is one such data structure highly used compared to other data structures while programming in python. It is because the list provides a wide range of features, and it is easily compatible with storing massive data under a single variable. To learn more about lists data structure, visit our article "5 Ways to Convert Set to List in Python".
As lists are the programmer's first choice to work with, in this article, we will study how to concatenate lists in python externally using various methods. We will also refer to respective examples of each method for your clear understanding. So, let’s get started!
How to Concatenate a List in Python?
Concatenation is the process of combining elements of a particular data structure in an end-to-end manner. Here we are talking about list data structure, and hence, we will join the elements of one list to the end of another list. Below is the 6 method by which python concatenate lists:
1) Using concatenation operation
The most conventional method to concatenate lists in python is by using the concatenation operator(+). The “+” operator can easily join the whole list behind another list and provide you with the new list as the final output as shown in the below example.
For example:
sample_list1 = [0, 1, 2, 3, 4] sample_list2 = [5, 6, 7, 8] result = sample_list1 + sample_list2 print ("Concatenated list: " + str(result))
Output:
Concatenated list: [0, 1, 2, 3, 4, 5, 6, 7, 8]
2) Naïve method
In this method, you have to traverse through the elements of the second list using for loop and then append those elements at the end of the first list. To append the elements, we can use the python in-built method append() as shown in the below example. As the elements are appended at the end of first lists, the program will return the first lists itself as the final output.
For example:
sample_list1 = [0, 1, 2, 3, 4] sample_list2 = [5, 6, 7, 8] for a in sample_list2 : sample_list1.append(a) print ("Concatenated list: " + str(sample_list1))
Output:
Concatenated list: [0, 1, 2, 3, 4, 5, 6, 7, 8]
3) Using the list of comprehensive
List comprehension is the process of generating the list elements depending on the existing list. You can easily concatenate two lists in python using the list comprehensive technique. Here, you can make use of for loop to traverse through the list element and later concatenate it, as shown in the below example.
For example:
sample_list1 = [0, 1, 2, 3, 4] sample_list2 = [5, 6, 7, 8] result = [b for a in [sample_list1, sample_list2] for b in a] print ("Concatenated list: " + str(result))
Output:
Concatenated list: [0, 1, 2, 3, 4, 5, 6, 7, 8]
4) Using extend() method
Python provides the in-built function extend() which can help you to concatenate two lists together. This function helps you to perform the inplace extension of the first list by iterating over the passed parameter and joining the elements of the list in a linear fashion.
For example:
sample_list1 = [0, 1, 2, 3, 4] sample_list2 = [5, 6, 7, 8] sample_list1.extend(sample_list2) print ("Concatenated list: " + str(sample_list1))
Output:
Concatenated list: [0, 1, 2, 3, 4, 5, 6, 7, 8]
5) Using the ‘*’ operator
“*” operator in python helps to unpack the collection of items in any data structure. Using the “*” operator, you can concatenate any number of lists, and hence the new list is returned as output. Remember, this method is only used in python version 3.6+.
For example:
sample_list1 = [0, 1, 2, 3, 4] sample_list2 = [5, 6, 7, 8] result = [*sample_list1, *sample_list2] print ("Concatenated list: " + str(result))
Output:
Concatenated list: [0, 1, 2, 3, 4, 5, 6, 7, 8]
6) Using itertools.chain() method
Python possesses a large range of library collections one of which is “itertools”. Itertools library possesses the in-built function “chain()” which helps python concatenate lists easily. The itertools.chain() method accepts various data structures and results into the linear sequence as the output.
Remember that the data type of the element does not affect the working of the chain() method. Also, you have to import the itertools library using the “import” keyword as shown in the below example:
For example:
import itertools sample_list1 = [0, 1, 2, 3, 4] sample_list2 = [5, 6, 7, 8] result = list(itertools.chain(sample_list1, sample_list2)) print ("Concatenated list: " + str(result))
Output:
Concatenated list: [0, 1, 2, 3, 4, 5, 6, 7, 8]
Conclusion
As lists are primarily used data structure while programming, often you face the situation of combining two or more lists. In this article, we have presented 6 standard methods by which you can easily concatenate lists in python along with examples and output. It is highly recommended to learn and understand this method to make your programming efficient.