Append one List to another List in Python
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)
Output:
[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
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)
Output:
[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)
Output:
[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)
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]
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
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK