How to concatenate two lists in Python?

In this article, we will discuss different ways to koin or merge two lists in Python.

Table Of Contents

Introduction

Suppose we have two lists i.e.

firstList = [11, 12, 13, 14, 15]
SecondList = [35, 36, 37, 38, 39, 40]

We want to containate these two lists into a single list. The final contents of the concatenated list should be,

[11, 12, 13, 14, 15, 35, 36, 37, 38, 39, 40]

There are different ways to do this. Let’s discuss them one by one,

Concatenate two lists using + operator in python.

We can use the + operator with two lists. It will merge the contents of both lists into a new list. Order of elements will also remain as it is. For example,

firstList = [11, 12, 13, 14, 15]
SecondList = [35, 36, 37, 38, 39, 40]

# Concatenate two lists into a new list
mergedList = firstList + SecondList

print(mergedList)

Output:

[11, 12, 13, 14, 15, 35, 36, 37, 38, 39, 40]

We created a new list with the merged contents.

Concatenate two lists using append()

In Python, the list class provides a function append(). It accepts an argument and adds that element to the end of list. We can use this function to concatenate the contents of two lists. For that we will iterate over all the elements of both the list and add them to a new list using the append() function. For example,

firstList = [11, 12, 13, 14, 15]
SecondList = [35, 36, 37, 38, 39, 40]

# Create an empty list
mergedList = []

# Append elements of first list to new list
for elem in firstList:
    mergedList.append(elem)

# Append elements of second list to new list
for elem in SecondList:
    mergedList.append(elem)

print(mergedList)

Output:

[11, 12, 13, 14, 15, 35, 36, 37, 38, 39, 40]

Here, we created a new list, with the merged contents. If you want to extend one of the existing list by adding elements of another list, then we also do that using the append() function. Let’s see an example, where we will append the contents of second list to the first list.

firstList = [11, 12, 13, 14, 15]
SecondList = [35, 36, 37, 38, 39, 40]

# Append elements of second list to first list
for elem in SecondList:
    firstList.append(elem)

print(firstList)

Output:

[11, 12, 13, 14, 15, 35, 36, 37, 38, 39, 40]

It added all the elements from second list, to the first list.

Concatenate two lists using extend()

In Python, list class provides a function extend(), to extend the contents of a list. It accepts an iterable sequence as an argument and extends the list by appending elements from that iterable sequence.

We can use the extend() function to append the contents of second list to the first list. For example,

firstList = [11, 12, 13, 14, 15]
SecondList = [35, 36, 37, 38, 39, 40]

# Append elements of second list to first list
firstList.extend(SecondList)

print(firstList)

Output:

[11, 12, 13, 14, 15, 35, 36, 37, 38, 39, 40]

Concatenate two lists using List comprehension

We can use a List comprehension to iterate over all the elements of two lists and create a new list from them. For example,

firstList = [11, 12, 13, 14, 15]
SecondList = [35, 36, 37, 38, 39, 40]

# concatenate two lists
mergedList = [elem for subList in [firstList, SecondList] for elem in subList]

print(mergedList)

Output:

[11, 12, 13, 14, 15, 35, 36, 37, 38, 39, 40]

Concatenate two lists using * operator

We can use the astrik to expand the contents of both the lists. Then we can create a new list with the content. For example,

firstList = [11, 12, 13, 14, 15]
SecondList = [35, 36, 37, 38, 39, 40]

# concatenate two lists
mergedList = [*firstList, *SecondList]

print(mergedList)

Output:

[11, 12, 13, 14, 15, 35, 36, 37, 38, 39, 40]

Concatenate two lists using itertools

We can use the itertools.chain() function to merge the contents of both the lists into a chained object. Then we can convert this chained object to a list. This list will contain the merged content. For example,

import itertools

firstList = [11, 12, 13, 14, 15]
SecondList = [35, 36, 37, 38, 39, 40]

# concatenate two lists
mergedList = list(itertools.chain(firstList, SecondList))

print(mergedList)

Output:

[11, 12, 13, 14, 15, 35, 36, 37, 38, 39, 40]

Summary

We learned different ways to concatenate the contents of two lists in Python. Happy Coding.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top