Lists are changeable and ordered, and it also allows duplicate values. The values are enclosed in square brackets.
You can concatenate Lists to one single list in Python using the +
operator.
In this tutorial, you’ll learn the different methods available to concatenate lists in python and how the different methods can be used in different use-cases appropriately.
If you’re in Hurry
You can use the following code snippet to concatenate two lists in Python.
Code
odd_numbers = [1, 3, 5, 7, 9]
even_numbers = [2, 4, 6, 8, 10]
numbers = odd_numbers + even_numbers
print("Concatenated list of Numbers : \n \n" + str(numbers))
+
operator concatenates the two lists and creates a new list object as a resultant object.
Output
Concatenated list of Numbers :
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
If You Want to Understand Details, Read on…
In this tutorial, you’ll learn the different methods available to join multiple lists to one list object.
Table of Contents
Using + Operator
You can join two or multiple list objects into one list object using the +
operator.
You can use this method when you want to create a new list object rather than adding the items of one list to the existing list. This is also the fastest method for concatenating lists.
Code
odd_numbers = [1, 3, 5, 7, 9]
even_numbers = [2, 4, 6, 8, 10]
numbers = odd_numbers + even_numbers
print("Concatenated list of Numbers : \n \n" + str(numbers))
Output
You’ll see the two lists, odd_numbers
and even_numbers
, concatenated into a single list called numbers
.
Concatenated list of Numbers :
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Using Extend()
The extends concatenate two lists by adding items from one list to another
- Extends an existing list with one or more items
- It increases the length of the list by the number of elements passed
You can use this method if you want to add more than one element to an existing list at one shot.
Code
odd_numbers = [1, 3, 5, 7, 9]
even_numbers = [2, 4, 6, 8, 10]
odd_numbers.extend(even_numbers)
print("Concatenated list of Numbers : \n \n" + str(odd_numbers))
Output
You can see all the items on the list even_numbers
is added to the existing list odd_numbers
at one shot using the odd_numbers.extend(even_numbers)
statement.
Concatenated list of Numbers :
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Using Append()
The append()
method adds one element to an existing list.
- Cannot add more than one item at once using the
append()
method. - To add more than one item using
append()
, you need to use the for loop to append each item.
Use this method to append the list of items to an existing list object instead of creating a new list.
Code
odd_numbers = [1, 3, 5, 7, 9]
even_numbers = [2, 4, 6, 8, 10]
for i in even_numbers:
odd_numbers.append(i)
print("Concatenated list of Numbers : \n \n" + str(odd_numbers))
Output
You can see that all the items in the even_numbers
list are added to the list odd_numbers
using the append()
method one by one.
Concatenated list of Numbers :
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Using * Operator
*
operator is python unpacks the items in the collections into a positional argument.
This is introduced in version 3.6.
You can check the python version using the below code.
Script
import sys
print(sys.version)
Output
3.8.2 (default, Sep 4 2020, 00:03:40) [MSC v.1916 32 bit (Intel)]
If your version is greater than or equal to version 3.6
, then you can use this method.
When you use the *
operator in a list,
- It unpacks the elements in the list so that you can use the items directly.
- Need not iterate over the list again to access the item.
You can concatenate multiple lists into one list by using the *
operator.
For Example, [*list1, *list2]
– concatenates the items in list1
and list2
and creates a new resultant list object.
You can use this method when you want to concatenate multiple lists into a single list in one shot.
Code
zero = [0]
odd_numbers = [1, 3, 5, 7, 9]
even_numbers = [2, 4, 6, 8, 10]
numbers = [*zero, *odd_numbers, *even_numbers]
print("Concatenated list of Numbers : \n \n" + str(numbers))
Output
You can see the lists zero
, odd_numbers
and even_numbers
concatenated into one single list object.
Concatenated list of Numbers :
[0, 1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
This is how you can concatenate multiple list objects into one single list object using the *
operator.
Using List Comprehension
List comprehension provides a short syntax to create a new list based on the values in the existing list.
You need to use two for
loops in list comprehension to concatenate two lists into one.
Example: y for x in [odd_numbers, even_numbers] for y in x
where,
for x in [list_1, list_2]
–For
loop to iterate over two lists one by one by one and add the result to a variablex
for y in x
– To iterate over the result variable x and create a new list out of the list comprehension.
Code
Use the following code to concatenate two lists into a single list using list comprehension.
odd_numbers = [1, 3, 5, 7, 9]
even_numbers = [2, 4, 6, 8, 10]
numbers = [y for x in [odd_numbers, even_numbers] for y in x]
print("Concatenated list of Numbers : \n \n" + str(numbers))
Output
You can see the lists odd_numbers
and even_numbers
concatenated into a single list called numbers
.
Concatenated list of Numbers :
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Concatenate List of Lists
Lists of lists in python is where a list item consists of another list. You can concatenate such a list of lists using the itertools.chain.fromiterable()
method.
Code
Use the following code to concatenate the list of lists into one single object.
- The list contains one list
['a', 'b']
as one object and just'c'
as another object. - When you use it with
itertools
, all three items will be concatenated to another single list.
import itertools
listoflists = [['a','b'], ['c']]
print(list(itertools.chain.from_iterable(listoflists)))
Output
['a', 'b', 'c']
This is how you can concatenate a list of lists in python using the itertools()
method.
Merge Lists Only Unique Items
You can merge two lists into one single list with only unique items in it using the set()
method. Because python set()
is used to create a list of items with unique objects, it doesn’t allow duplicates.
- Pass the list to a set, it removes the duplicates automatically.
- Concatenate two lists using the
+
operator and pass the resultant list to theset()
method to remove duplicates. - To create a list with unique items, you can pass the set to the
list()
method again.
Code
Use the following code to merge lists only with unique items.
- Both the source list contains
0
in them. - This needs to be removed while merging two lists.
odd_numbers = [0, 1, 3, 5, 7, 9]
even_numbers = [0, 2, 4, 6, 8, 10]
numbers = list(set(odd_numbers + even_numbers))
print("Concatenated list of Numbers (Only Unique Values) : \n \n" + str(numbers))
Output
You can see the duplicate element 0 is available only once in the resultant list.
Concatenated list of Numbers (Only Unique Values) :
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Concatenate Two Lists Side By Side
During the normal concatenation operation,
- All the items in the second list are appended to the items in the first list, and so on.
- The order of the elements in each list appears the same.
At times, you may need to concatenate items from each list side by side or element-wise.
For Example, you need to add the first elements of the list first, then the second element of the list to be added, then the third element of the list is added, and so on.
- You can do this by using the list comprehension and zip() function.
- The
zip()
function is used to create an iterator of tuples with the first item of each iterator is paired together, then the second item of the iterator is paired together, then the third item, and so on. - Use the
join()
method to join together all the paired tuples into one single list.
Code
Use the following code to concatenate two lists side by side to create an order of numbers.
odd_numbers = ['1', '3', '5', '7', '9']
even_numbers = ['2', '4', '6', '8', '10']
numbers = [' '.join(x) for x in zip(odd_numbers,even_numbers)]
print("Concatenated list of Numbers Side By side : \n \n" + str(numbers))
You can see the list created with the odd numbers and even numbers merged in order.
Output
Concatenated list of Numbers Side By side :
['1 2', '3 4', '5 6', '7 8', '9 10']