Append one list to another list in Python

In this article, we will discuss different ways to append all the elements of a list to an another list in Python. You can also say that we are going to learn how to concatenate two lists in Python.

Table Of Contents

Append one list to another list using extend()

In Python, the list class provides a function extend(). It accepts a sequence as an argument, and appends all its elements to the end of calling list object. For example,

firstList  = [11, 12, 13, 14, 15]
secondList = [46, 47, 48, 49, 50]

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

print(firstList)

Output:

[11, 12, 13, 14, 15, 46, 47, 48, 49, 50]

Add elements of a list to an another list using + Operator

We can directly use the + operator to concatenate two lists and store all the merged contents to a new list. For example,

firstList  = [11, 12, 13, 14, 15]
secondList = [46, 47, 48, 49, 50]

# Concatenate lists
mergedList = firstList + secondList

print(mergedList)

Output:

[11, 12, 13, 14, 15, 46, 47, 48, 49, 50]

We can also store the merged contents to one of the original list.

Add elements of a list to an another list using for loop

Create a empty list. Then iterate over all elements of first list and add them to the new list one by one. After that, iterate over all elements of second list and add them to the new list one by one. For example,

firstList  = [11, 12, 13, 14, 15]
secondList = [46, 47, 48, 49, 50]

mergedList = []

# Iterate over first list and
# append all element to the new list
for elem in firstList:
    mergedList.append(elem)


# Iterate over second list and
# append all element to the new list
for elem in secondList:
    mergedList.append(elem)

print(mergedList)

Output:

[11, 12, 13, 14, 15, 46, 47, 48, 49, 50]

We created a new list with the merged contents.

If you want to add the contents of second list to the first list, then follow the below example,

firstList  = [11, 12, 13, 14, 15]
secondList = [46, 47, 48, 49, 50]

# Iterate over second list and
# append all element to the firstList list
for elem in secondList:
    firstList.append(elem)

print(firstList)

Output:

[11, 12, 13, 14, 15, 46, 47, 48, 49, 50]

Here, we added all the elements of second list to the first list.

Append elements of a list to an another list using astriks

Expand both the list objects to individual elements using astrik, and create a new list with them. Then assign this new list to one of the original list variable. For example,

firstList  = [11, 12, 13, 14, 15]
secondList = [46, 47, 48, 49, 50]

# Expand lists to individual elements
# and create a new list with merged content
firstList = [*firstList, *secondList]

print(firstList)

Output:

[11, 12, 13, 14, 15, 46, 47, 48, 49, 50]

Here, we added all the elements of second list to the first list.

Append elements of a list to an another list using itertools

Use the chain() method of itertools module to chain two list objects. This chain will yield elements from first list, and when its exhauted, then it will yield elements from second list. Store all these elements to a new list and assign that to the variable poiting to first list. It will give an effect that we have added all the elements of second list to the first list. For example,

import itertools

firstList  = [11, 12, 13, 14, 15]
secondList = [46, 47, 48, 49, 50]

# Append elements of second list to first list
firstList = list(itertools.chain(firstList, secondList))

print(firstList)

Output:

[11, 12, 13, 14, 15, 46, 47, 48, 49, 50]

Here, we added all the elements of second list to the first list.

Summary

We learned different ways to append elements of a list to an another list in Python. Thanks.

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