In this Python tutorial, we’ll explore how to combine two or more lists in Python, and also we will understand several techniques to concatenate multiple lists in Python.
As a Developer, while making the Python Project, I got the requirement to combine two or more lists in Python.
Here we will see:
- How to Concatenate multiple lists in Python using + operator
- Concatenate multiple lists in Python using * operator
- How to Concatenate multiple lists in Python using extend()
- Concatenated multiple lists in Python using list comprehension method
- How to Concatenate multiple lists in Python using itertools.chain()
- How to Concatenate multiple lists in Python using append()
The process of joining the components of one list to the end of another list is known as concatenation. Let’s see how to concatenate two or multiple lists using different techniques in Python.
Methods to Concatenate multiple lists in Python
In Python, there are numerous methods for joining lists together. We’ll go through how to concatenate two lists in Python specifically utilizing the plus operator, list comprehension, and multiply operator.
How to Concatenate multiple lists in Python using + operator
- In this section, we will discuss how to concatenate multiple lists in Python by using the + operator.
- This is the simplest method for concatenation. In this, the concatenation of two or more lists is created, and the output is then placed in a new variable.
- Two lists can be joined together with the “+” operator. It creates a new list as output by appending one list to the end of the other list.
Syntax:
Here is the syntax of the + operator.
List1 + List2 + .... + ListN
Example:
Let’s see an example and check how to Concatenate multiple lists in Python by using the + operator.
Source Code:
# Creation of lists
Country_name = ["U.S.A", "China" , "Australia"]
Country_id = [5672, 3894, 13738]
Country_Zipcode =[85001, 71601, 90001]
# By using the + operator
result = Country_name + Country_id+ Country_Zipcode
# Display the Content
print("Concatenated list :",result)
In the following given code first, we initialized three input lists by [] brackets. Next, we concatenated the given lists by using the ‘+’ operator.
Here is the implementation of the following given code.
This is how to Concatenate multiple lists in Python by using the + operator.
Read: Python list methods
Concatenate multiple lists in Python using * operator
- Here, we will discuss How to Concatenate multiple lists in Python using the * operator in Python.
- Python ‘*’ operator makes extracting a group of objects in any data type easier. The resulting list is returned as a result when you concatenate any number of lists using the “*” operator.
Example:
Let’s take an example and check How to Concatenate multiple lists in Python using the * operator in Python.
Source Code:
States_in_USA = ['Arkansas', 'California', 'Colorado']
Zip_Code = [71601, 90001, 80001]
new_output = [*States_in_USA, *Zip_Code]
print ("Concatenated list: " ,new_output)
In the above code first, we declared two lists and then combined the lists with the * operator and the result will return the concatenated list.
You can refer to the below screenshot.
In this example, we have understood How to Concatenate multiple lists in Python by using the * operator.
Read: Python concatenate list
How to Concatenate multiple lists in Python using extend()
- Two lists in Python can be concatenated using the extend() technique. The extend() function adds the element to the list after iterating over the provided parameter.
- This method is a built-in function in Python and this function is easy to iterate over an iterable (string, tuple, list, set, or dictionary) and add each item to the end of the input list by using the list.extend() method.
Syntax:
Let’s have a look at the Syntax and understand the working of extend() method in Python.
list.extend(iterable)
Note: It is essential to provide an iterable parameter, which can be a set, tuple, or list. List extend() adds the elements to the already-existing list but does not return any value.
Example:
Here we will take an example and understand how we can concatenate multiple lists in Python by using the extend method.
Source Code:
# Creation of two lists
new_val_1 = [563, 3647, 9477, 123, 8374]
new_val_2 = [423, 7453, 81234, 6754, 6732]
# By using the list.extend() method
new_val_1.extend(new_val_2)
# Display the list
print ("Concatenated list : ", (new_val_1))
In the above code first, we created two input lists and then used the extend() function for concatenating two input lists.
Here is the execution of the following given code.
As you can see in the Screenshot we have discussed How to Concatenate multiple lists in Python by using the extend method.
Read: How to Reverse a List in Python
Concatenated multiple lists in Python using list comprehension method
- List Comprehension is a different way to concatenate two lists in Python. Basically, list comprehension involves creating a list of elements based on an already existing list.
- A list can be generated from any existing iterable object in a simple way using list comprehension and it is used to create lists using
for
loop in a different way.
Syntax:
Here is the Syntax of the list comprehension method for concatenating the list in Python.
n for m in [list1, even_numbers] for n in m
Example:
Let’s take an example and check how to concatenate multiple lists in Python by using the list comprehension method.
Source Code:
# Creation of input list
Cars_in_USA = ['Tesla','BMW','Ford']
Bikes_in_USA = ['Kawasaki','Harley Davidson', 'Yamaha']
values=[234,334,556]
# By using the list comprehension method
new_output = [item for m in [Cars_in_USA, Bikes_in_USA, values] for item in m]
# Display the Content
print("Concatenated list :",new_output)
In the above code first, we created an input list named “Cars_in_USA”, “Bikes_in_USA”, and “values”. Next, we combined these input lists and stored them in a ‘new_output’ variable.
Here is the screenshot of the following given code.
This is how to concatenate multiple lists in Python by using the list comprehension method.
Read: How to add string to list Python
How to Concatenate multiple lists in Python using itertools.chain()
- In this section, we will discuss How to Concatenate multiple lists in Python by using the itertools.chain() method.
- The itertools.chain() method extracts a linear sequence of items from data of various iterables, such as lists, strings, tuples, etc.
- It basically creates a single sequence out of all the iterables and returns a single iterator to that sequence.
Syntax:
Let’s have a look at the syntax and understand the working of itertools.chain() method in Python.
itertools.chain(list1, list2, ...., listN)
Note: In this example, iterable will be the input list.
Example:
Here we will take an example and check how to concatenate multiple lists in Python by using the itertools.chain() method.
Source Code:
import itertools
# Creation of input list
integer_values = [563, 2344, 9744,1674, 9345]
floating_values = [6.3, 8.2, 6.3, 11.7, 89.2]
# By using the itertools.chain() method
new_output = list(itertools.chain(integer_values, floating_values))
# Display the new list
print ("Concatenated new list : ", new_output)
In the following given code first, we imported the itertools module and then created the input lists, and then used the itertools.chain() method and within this method, we passed the input lists as an argument.
You can refer to the below screenshot.
Read: How to Python Append List to Another List
How to Concatenate multiple lists in Python using append()
- In this example, we will discuss How to Concatenate multiple lists by using the append() method in Python.
- The append() method in Python adds a new item to the list’s end. By updating the list, it adds an element. The method doesn’t give itself back.
Syntax:
Here is the syntax of the list.append() method in Python.
list.append(item)
Note: The item parameter defines the elements to be added at the end of the list.
Example:
# First input list
Country_name = ['China', 'Australia', 'Germany']
# Second input list
Country_id = [6735, 9845, 4573]
# appending Country_name to Country_id list
Country_name.append(Country_id)
# Display the Content
print('Concatenated list : ', Country_name)
In the following given code, we have created two input lists and then for concatenating the list we use the list1.append(list2) method and it will display the combining list.
Here is the implementation of the following given code.
You may also like to read the following Python tutorials.
- How to insert item at end of Python list
- How to find smallest number in a Python list
- How to get string values from list in Python
- Concatenate all elements of list into string in Python
- What is Python __init__ method [With Examples]
- How to get unique values from a list in Python
In this Python tutorial, we covered six methods for concatenating two lists in Python: plus, list comprehension method, multiply, append(), itertools.chain(), and extend().
- How to Concatenate multiple lists in Python using + operator
- Concatenate multiple lists in Python using * operator
- How to Concatenate multiple lists in Python using extend()
- Concatenated multiple lists in Python using list comprehension method
- How to Concatenate multiple lists in Python using itertools.chain()
- How to Concatenate multiple lists in Python using append()
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.