Let’s see how to concatenate two lists using different methods in Python. This operation is useful when we have numbers of lists of elements which needs to be processed in a similar manner.
Method #1 : Using Naive Method
In this method, we traverse the second list and keep appending elements in the first list, so that first list would have all the elements in both lists and hence would perform the append.
Concatenated list using naive method : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Method #2 : Using + operator
The most conventional method to perform the list concatenation, the use of “+” operator can easily add the whole of one list behind the other list and hence perform the concatenation.
Concatenated list using + : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Method #3 : Using list comprehension
List comprehension can also accomplish this task of list concatenation. In this case, a new list is created, but this method is a one liner alternative to the loop method discussed above.
Concatenated list using list comprehension: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Method #4 : Using
extend() is the function extended by lists in Python and hence can be used to perform this task. This function performs the inplace extension of first list.
Concatenated list using list.extend() : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Method #5 : Using * operator
Using * operator, this method is the new addition to list concatenation and works only in Python 3.6+. Any no. of lists can be concatenated and returned in a new list using this operator.
Concatenated list using * operator : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Method #6 : Using
itertools.chain() returns the iterable after chaining its arguments in one and hence does not require to store the concatenated list if only its initial iteration is required. This is useful when concatenated list has to be used just once.
Concatenated list using itertools.chain() : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
- Python | Concatenate two lists element-wise
- Python | Concatenate dictionary value lists
- Python | Ways to concatenate boolean to string
- Python | Ways to concatenate tuples
- Python | Zipping two lists of lists
- Python program to concatenate two Integer values into one
- Concatenate two strings using Operator Overloading in Python
- Concatenate two columns of Pandas dataframe
- How to Concatenate two 2-dimensional NumPy Arrays?
- Python | Program to count number of lists in a list of lists
- Python - Convert Lists into Similar key value lists
- Python | Pandas Series.str.cat() to concatenate string
- Python | Merge, Join and Concatenate DataFrames using Panda
- Python | Numpy np.ma.concatenate() method
- Python | How to Concatenate tuples to nested tuples
- Python | Concatenate N consecutive elements in String list
- Python - Concatenate Kth element in Tuple List
- Python - Concatenate consecutive elements in Tuple
- Python | Concatenate All Records
- Python - Concatenate Dictionary string values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai