In this python tutorial, you will learn how to combine two lists. By combine, we mean joining the two lists together.
The result will be a new list in which all items in the first list occur before all items in the second list.
The idea can be illustrated using the following code:
lst1 = ["Mozambique", "India", "China", "Romania", "Russia"]
lst2 = ["United States", "Spain", "South Africa", "Brazil", "Chile", "Japan"]
print(combine(lst1, lst2))
The code has two lists of strings; each list contains the names of countries. The function doing the combination work is combine()
. When this code is executed, we get the output:
Output:
['Mozambique', 'India', 'China', 'Romania', 'Russia', 'United States', 'Spain', 'South Africa', 'Brazil', 'Chile', 'Japan']
This first output shows the combined lists; elements of lst1
appear before elements of lst2
. When the order of the arguments is reversed; that is;
print(combine(lst2, lst1))
The order of the elements in the output will be reversed. Items in lst2 will appear before items in lst1 in the final output:
Output:
['United States', 'Spain', 'South Africa', 'Brazil', 'Chile', 'Japan', 'Mozambique', 'India', 'China', 'Romania', 'Russia']
There are several ways to combine lists in python. In our code examples, we will be defining each method in a function, combine()
.
def combine(list1, list2):
# initialize the results list
result_list = []
# the algorithm comes next...
# return the results list
return result_list
combine()
takes two list arguments, and returns the two lists combined as a single list. A separate result_list is used so that we do not modify list1 while trying to combine the two lists. The following sections explore each of these techniques in turn.
Note: Read the article if you want to print list without brackets.
Combine two lists using append() method and for loop in Python
This first approach is a naive way to combine two lists. The approach uses a for-loop and the list append()
method.
def combine(list1, list2):
# initialize the results list
result_list = []
# the algorithm comes next...
# create a copy of list1 and store it in result_list
result_list = list(list1)
# append the contents of the second list to
# the results list
for item in list2:
result_list.append(item)
# return the results list
return result_list
This approach uses a for loop and the append()
function to combine the list parameters passed into the function. The algorithm first creates a copy of list1
and stores it in result_list
. The second loop then appends list2
to result_list
.
Use the Python + operator to combine elements of lists
The + operator acts in a similar way to the + operator for strings; the two lists are concatenated, or combined, together
def combine(list1, list2):
# initialize the results list
result_list = []
# the algorithm comes next...
# concatenate the two lists using the + operator
result_list = list1 + list2
# return the results list
return result_list
The + operator returns a new list containing the items in the previous two lists concatenated together.
Using extend() method
Python lists have an extend()
method that can be used to append the contents of a second list to the end of a first list.
Note that extend()
appends the contents of the list, not the list itself, to a previous list. We can also use extend()
to combine the contents of two lists. The following code shows how the combine()
method can be modified to make use of extend()
.
def combine(list1, list2):
# initialize the results list
result_list = []
# the algorithm comes next...
# create a copy of list1 and store it in result_list
result_list = list(list1)
# extend result_list with list2
result_list.extend(list2)
# return the results list
return result_list
Combine two lists by using list unpacking and packing
The * operator in front of a list unpacks the list. However, it has to appear within another list or python will report an error. The next example shows what this means.
def combine(list1, list2):
# initialize the results list
result_list = []
# the algorithm comes next...
# unpack the contents of the two lists and pack the contents into a new list
result_list = [*list1, *list2]
# return the results list
return result_list
We first unpacked the two lists we needed to combine, then packed them into a new list to get a combined list.
Combine two lists by using itertools.chain()
Function chain()
in the itertools
module combines several iterables into one iterable. We can use this functionality to combine several lists into one list.
from itertools import chain
def combine(list1, list2):
# initialize the results list
result_list = []
# the algorithm comes next...
# chain the contents of the two lists
chain_iter = chain(list1, list2)
# convert the iterable returned by chain() into a list
result_list = list(chain_iter)
# return the results list
return result_list
When chain()
is called, it returns an object of type chain
. Calling chain()
directly is not very informative because it does not show us what has been accomplished. However, converting the return value of chain()
to a list will help us get the desired result as a list.
Conclusion
That is all for this tutorial. We hope the techniques presented here will prove useful in your coding exercises. Please check out our other tutorials on python.
Want to help with python homework for more such awesome python projects? Feel free to contact us.