How To Concatenate Two Lists In Python?

Python List Concatenation

This article is a quick guide on how you can concatenate two or more lists into one single list in Python. There are several methods for list concatenation.

Python List Concatenation Using + Operator

In Python, one can simply use the + operator to concatenate any number of lists together as demonstrated below. The code for the same is written below and then the output is displayed in the screenshot.

Example Code.


Python Concatenate List Using + Operator

You can also use the += operator for appending a new list to an existing list as expressed in the code given below.



Appending Lists In Python

Concat Lists Using List Literal (Only For Python 3.5 or higher)

In Python version 3.5 or higher, you can also concatenate lists using list literal as demonstrated in the example code given below.

Example Code.


Concatenate Lists In Python Using List Literals

List Concatenation In Python For A Unique List

This is a very useful thing for Python List Concatenation. There might be some of the same values in two different lists that you wanted to merge or concatenate. But in the resultant concatenated list you might only require the unique merged values out of both lists. Then this solution that uses the set function is going to help you.

In the following code, there are two lists. Both of these lists have some common values. In the merged or concatenated lists we do not want the values to be repeated. So, here we wanted to have concatenated lists with unique values.



Unique Python List Concatenation

Merging Python Lists Using Extend Function

This is another great way to extend or merge Python lists and can help in several different use cases.



Extend Python Lists

Related Articles.

There are numerous other methods and tricks to concatenate or merge lists in python. If you know any better and easy way, let our visitors know in the comments section.

I hope as a beginner, you found this article useful. If so, do share it with others who might get benefits from this article as well. Feel free to ask any kind of questions related to this article in the comments section.

Also, don’t forget to Subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *