The list
is a container data type that represents an ordered collection of elements.
list concatenation is a process of joining two or more lists together in an end-to end manner to form a single list.
There are various ways that we can achieve list concatenation in an efficient way:
- Using concatenation operator( + )
- Using the
extend()
method. - Using
itertools.chain()
function. - Using the unpacking operator(
*
) - Using list comprehension.
Using the concatenation operator
When the + operator is used with two lists, the elements of the two lists are combined to form one larger list.
list3 = list1 + list2
In the newly formed list, all the elements of the first list appear before the elements of the second list.
list1 = [10, 10, 30, 40, 50]
list2 = [100, 200, 300]
result = list1 + list2
print(result)
Using the extend() method
The extend()
methods of lists is used to extend a list by adding all items from an iterable given as argument.
list1.extend(list2)
In this approach, there is no creation of a new list, instead the elements of list2
are appended at the end of list1
.
list1 = ['Python', 'Java', 'C++']
list2 = ['PHP', 'Ruby', 'HTML']
list1.extend(list2)
print(list1)
In the above example, we called the extend()
method of list1
with list2
as the argument. The outcome is that all the elements of list2 are added at the end of list1
while list2
remains unchanged.
Using itertools.chain()
The itertools
module in the standard library offer a number of tools for working with and manipulating iterable objects.
The chain()
function in the module is used to create an iterator object that iterates through multiple iterables as if they were one continuous sequence. This approach is more efficient than loops because the iterables are not loaded into memory all at once.
chain(list1, list2, list3,..............)
We can then use builtin list() function with the returned iterator as the argument to create a new list containing the elements of all the input lists.
from itertools import chain
list1 = ['Python', 'C++']
list2 = ['Java', 'PHP']
list3 = ['Ruby', 'Swift']
mylist = list(chain(list1, list2, list3))
print(mylist)
Using the unpacking operator
The *
operator is used in some cases to unpack the contents of a data structure or sequence into individual variables. Used this way, it is known as the unpacking operator.
mylist = [*list1, *list2]
The unpacking operation makes the individual elements of each list to be added to the new list.
list1 = ['one', 'two', 'three']
list2 = ['four', 'five']
mylist = [*list1, *list2]
print(mylist)
In the above example, the statement res = [*list1, *list2] replaces the list1 and list2 with the items in the given order i.e. elements of list1 after elements of list2.
Using list comprehension
List comprehension is a convenient syntax that combines the features of looping, conditional execution and sequence building all into one concise syntax.
We can use list comprehension to create a new list containing the combined elements of the input lists.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
mylist = [i for lst in (list1, list2) for i in lst ]
print(mylist)
In the above example, we use list comprehension to combine the lists by looping over each of the two lists and appending each item to the new list.