2

Append one List to another List in Python

 2 years ago
source link: https://thispointer.com/append-one-list-to-another-list-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

This article will discuss different ways to append elements of one list to another list in Python.

Table Of Contents

Append one list to another using extend() function

In Python, the list provides a method extend(iterable). It accepts an iterable sequence as an argument and appends all the elements of iterable to the calling list object. Let’s use this to add elements of one list to another, for example,

first = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]
# Append elements of list 'second' to the list 'first'
first.extend(second)
print(first)
first  = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]

# Append elements of list 'second' to the list 'first' 
first.extend(second)

print(first)

Output:

[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72]
[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72]

It modified the calling list object “first” by adding all the elements from list “second” to the list “first”.

Advertisements

vid5e62792b95ec8618094391.jpg?cbuster=1600267117
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY0NwM1OTEmMCZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDMmMxQmMDM0NUYmMDMmN0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3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mNvUlMCUlOEgIVE1MJTJDJTIjoGyeZSUlMEqyY2giJTI5JTIjQ2ulo21yJTJGNmphMC4mODY1LwElMCUlMFNuZzFlnSUlRwUmNl4mNvZwp3V1nWQ9NwIlMTplNTx1Mmt5ZCZwo250ZW50RzyfZUyxPTAzoWVxnWFQoGF5TGymqEyxPTAzoWVxnWFMnXN0SWQ9MCZxqXI9ODAjJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NDYmNTxkMmx4ODxzqWyxPVNyn2yhZG9TUGkurWVlNwIlMTplNWE1MwFxOCZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZupHByozQgo25yLWkcp3QgqG8gYW5iqGuypv1fnXN0LWyhLXB5qGuiovUlRvZzoG9uqFN0YXR1pm1zYWkmZSZynWRmpD1jpzVvnWQ=

Append one or more lists using itertools.chain() function

Python provides module itertools, which contain functions to handle iterators. One of the functions in the itertools module is chain(*iterables). It accepts multiple iterables, groups them, and returns a new iterator. If we loop over the returned iterator object, it starts from the items from the first internal iterable. Once it is done with an internal iterable, it proceeds to the next internal iterable until all iterables are done. We can use this to merge two or more lists, for example,

import itertools
first = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]
third = [91, 92, 93, 94]
# Add elements of all the three lists to a new list
final_list = list(itertools.chain(first, second, third))
print(final_list)
import itertools

first  = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]
third  = [91, 92, 93, 94]

# Add elements of all the three lists to a new list
final_list = list(itertools.chain(first, second, third))

print(final_list)

Output:

[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72, 91, 92, 93, 94]
[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72, 91, 92, 93, 94]

We passed three lists to the chain() function and converted the returned iterable to a new list. All the elements of the three lists got added to the new list.

Append one or more lists using + operator

We can also add two or more lists together using the + operator in Python. For example,

first = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]
third = [91, 92, 93, 94]
# Add elements of all the three lists to a new list
final_list = first + second + third
print(final_list)
first  = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]
third  = [91, 92, 93, 94]

# Add elements of all the three lists to a new list
final_list = first + second + third

print(final_list)

Output:

[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72, 91, 92, 93, 94]
[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72, 91, 92, 93, 94]

It is a simple and elegant solution to merge all the lists.

The complete example is as follows

first = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]
# Append elements of list 'second' to the list 'first'
first.extend(second)
print(first)
import itertools
first = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]
third = [91, 92, 93, 94]
# Add elements of all the three lists to a new list
final_list = list(itertools.chain(first, second, third))
print(final_list)
first = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]
third = [91, 92, 93, 94]
# Add elements of all the three lists to a new list
final_list = first + second + third
print(final_list)
first  = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]

# Append elements of list 'second' to the list 'first' 
first.extend(second)

print(first)

import itertools

first  = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]
third  = [91, 92, 93, 94]

# Add elements of all the three lists to a new list
final_list = list(itertools.chain(first, second, third))

print(final_list)


first  = [11, 12, 13, 14, 15, 16]
second = [77, 76, 75, 74, 73, 72]
third  = [91, 92, 93, 94]

# Add elements of all the three lists to a new list
final_list = first + second + third

print(final_list)

Output:

[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72]
[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72, 91, 92, 93, 94]
[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72, 91, 92, 93, 94]
[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72]
[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72, 91, 92, 93, 94]
[11, 12, 13, 14, 15, 16, 77, 76, 75, 74, 73, 72, 91, 92, 93, 94]

Summary:

We learned different ways to merge two or more lists in Python.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK