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
- Append one or more lists using itertools.chain() function
- Append one or more lists using + operator
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”.
Frequently Asked:
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,
Latest Python - Video Tutorial
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.
Latest Video Tutorials