How to add items from a list to another list in Python?

This tutorial will discuss about unique ways to add elements from a list to another list in Python.

Table Of Contents

Introduction

Suppose we have two lists,

Let’s see the complete example,

firstList = [11, 12, 13, 14, 15, 16]
secondList = [99, 98, 97, 96, 95]

We want to add all elements of second lists into the first list. After addition, contents of first list will be like,

Advertisements

Let’s see the complete example,

[11, 12, 13, 14, 15, 16, 99, 98, 97, 96, 95]

There are different ways to add all elements of a list into another list. Let’s discuss them one by one

Method 1: Using extend() function

In Python, the List class provides a function extend() to append multiple elements in list, in a single shot. The extend() function accepts an iterable sequence as an argument, and adds all the element from that sequence to the calling list object. Now, to add all elements of a second list to the first list, call extend()function on first list object, and pass the second list as argument to it.

Let’s see the complete example,

firstList = [11, 12, 13, 14, 15, 16]

secondList = [99, 98, 97, 96, 95]

# Add elements of secondList at the end of firstList
firstList.extend(secondList)

print(firstList)

Output

[11, 12, 13, 14, 15, 16, 99, 98, 97, 96, 95]

It added all the elements of second list, into the first list.

Method 2: Using for-loop

The most basic solution to add all elements of second list into first list, is to iterate over all elements of second list, and add them one by one at the end of first list using the append() function.

Let’s see the complete example,

firstList = [11, 12, 13, 14, 15, 16]

secondList = [99, 98, 97, 96, 95]

# Add elements of secondList at the end of firstList
for elem in secondList:
    firstList.append(elem)

print(firstList)

Output

[11, 12, 13, 14, 15, 16, 99, 98, 97, 96, 95]

It added all the elements of second list, into the first list.

Method 3: Using astrik and unpacking

Unpack the first and second list element contents as individual elements, and add them into a new list. This new list will have contents of both the lists. Then assign it back to the first list again.

Let’s see the complete example,

firstList = [11, 12, 13, 14, 15, 16]

secondList = [99, 98, 97, 96, 95]

# Add elements of secondList at the end of firstList
firstList = [*firstList, *secondList]

print(firstList)

Output

[11, 12, 13, 14, 15, 16, 99, 98, 97, 96, 95]

It added all the elements of second list, into the first list.

Method 4: Using itertools

Pass both the lists to itertools.chain() function of module itertools. It returns a chain object, which first yields elements from the first list, until it is exhausted, then elements from the next lists, until both the lists are exhausted. Then collect all these returned elements to new list, and assign that back to the variable poiting first list . This way we will update the first list.

Let’s see the complete example,

import itertools

firstList = [11, 12, 13, 14, 15, 16]

secondList = [99, 98, 97, 96, 95]

# Add elements of secondList at the end of firstList
firstList = list(itertools.chain(firstList, secondList))

print(firstList)

Output

[11, 12, 13, 14, 15, 16, 99, 98, 97, 96, 95]

It added all the elements of second list, into the first list.

Method 5: Using + Operator

We can also use the + operator with both the lists to create a list, with merged contents. Then assign this new merged list to the variable pointing to first list.

Let’s see the complete example,

firstList = [11, 12, 13, 14, 15, 16]

secondList = [99, 98, 97, 96, 95]

# Add elements of secondList at the end of firstList
firstList = firstList + secondList

print(firstList)

Output

[11, 12, 13, 14, 15, 16, 99, 98, 97, 96, 95]

It added all the elements of second list, into the first list.

Summary

We learned about different ways to add all elements of a list into another list in Python.

Advertisements

Thanks for reading.

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