Python Python Trim String Example

How to concatenate two lists in python


In this article, you are going to learn about how to concatenate two lists in Python.

In the Python programming language, lists are considered as the data structure that stores a group of elements. You can create a python list by using square brackets [] and may store different types of elements. Sometimes, you need to concatenate multiple lists elements into a single one. To perform this action python provides some useful options like (+) operator, extend() method, naive Method, list comprehension, itertools.chain() method. We will explore these approaches to concatenate two lists in python. Let’s start with the (+) operator.

(+) Operator

We can simply concatenate two lists by using the (+) operator and it is the most conventional approach to do so. It simply joins two lists into a single list and adds the elements of the second list just behind the first list. Let’s see the below code example:

student_list1 = ['Alex', 'Deven', 'Rohit']
student_list2 = ['Alina', 'Anik']

# Concatenate Two lists by using (+) Operator
final_list = student_list1 + student_list2

print(final_list)

# Output : ['Alex', 'Deven', 'Rohit', 'Alina', 'Anik']

Here, you can see that the two lists have been added into a single one and the process is very easy where all we need to do is to add those two lists by using a (+) operator in between them.

Extend() Method

The extend() method is also used to concatenate two lists in python. It simply adds the second list elements into the first list. That is also called the in-place concatenation. See the below code example:

student_list1 = ['Alex', 'Deven', 'Rohit']
student_list2 = ['Alina', 'Anik']

# Concatenate Two lists by using extend() method
student_list1.extend(student_list2)

print(student_list1)

# Output : ['Alex', 'Deven', 'Rohit', 'Alina', 'Anik']

Here, you can see that student_list1 has only three elements but we have merged two lists by using the extend() method.

Naive() Method

The naive() method concatenates two lists with the help of the for loop and the append() method. The for loop is used to traverse the second list and the append() method is used to add the elements of the second list into the first list. See the below code example:

student_list1 = ['Alex', 'Deven', 'Rohit']
student_list2 = ['Alina', 'Anik']

# Concatenate Two lists by using naive() method
for el in student_list2:
    student_list1.append(el)

print(student_list1)

# Output : ['Alex', 'Deven', 'Rohit', 'Alina', 'Anik']

Here, in the output, you can notice that the elements of two lists are being joined into a single list.

List Comprehension

In this approach, the for loop is also used to concatenate two lists in python but in a different manner. It simply creates a new list and shows the elements of the concatenated lists in the new list. See the below code example of the list comprehension

student_list1 = ['Alex', 'Deven', 'Rohit']
student_list2 = ['Alina', 'Anik']

# Concatenate Two lists by using List Comprehension method
final_list = [i for j in [student_list1,student_list2] for i in j]

print(final_list)

# Output : ['Alex', 'Deven', 'Rohit', 'Alina', 'Anik']

Here, we have used nested for loop and store the elements of both lists into the final_list and in the output, you can see the concatenated elements of those two lists.

Itertools.chain() Method

The itertools.chain() method is also used to concatenate two lists in the python programming language. It not only accepts the lists but also any iterable items like strings, tuples, etc. See the below code example:

import itertools
student_list1 = ['Alex', 'Deven', 'Rohit']
student_list2 = ['Alina', 'Anik']

# Concatenate Two lists by using itertools.chain() method
final_list = list(itertools.chain(student_list1,student_list2))

print(final_list)

# Output : ['Alex', 'Deven', 'Rohit', 'Alina', 'Anik']

Here, you can see that to use this method we need to import it first, and then we have used it by calling it, and finally, it returned a new list that is concatenated both lists.

These are some useful methods that are used to concatenated lists in python. You may follow any of these approaches based on your program’s need for concatenating two lists in python.


Share on social media

//