Being able to work with and manipulate Python lists using different techniques is a crucial skill to have to become a well-rounded Python developer.
In this tutorial, we will be learning about the various ways we can join/merge lists in Python.
By using the ‘+’ operator, append() function, for loops, and extend() function we can manipulate lists. Also, list unpacking allows us to perform further alterations. Furthermore, we will learn how to utilize the zip() function in conjunction with list comprehension to join lists.
Lastly, in addition to the aforementioned, we will be going over two modules: NumPy and itertools which provide additional means to accomplish the goal of merging lists.
Ways to Join Lists in Python
In this article, we will learn about two main topics surrounding joining lists together in Python.
- Joining lists without removing duplicate values.
- Joining lists by removing duplicate values
Joining Lists without Removing Duplicate Values
In this section, we will learn about eight different ways to join lists in Python without eliminating duplicate values.
1) Using the + Operator to Join Lists
One of the easiest ways to join lists is using the ‘+’ operator. When using the ‘+’ operator, the original lists do not get changed.
Code Sample:
list1 = ["Mehvish Ashiq", 1, 1, 23.34] list2 = [23.34, "Thomas Christopher"] list3 = list1 + list2 print("List1: ", list1) print("List2: ", list2) print("List3: ", list3)
OUTPUT:
List1: ['Mehvish Ashiq', 1, 1, 23.34] List2: [23.34, 'Thomas Christopher'] List3: ['Mehvish Ashiq', 1, 1, 23.34, 23.34, 'Thomas Christopher']
When joining two lists this way, the contents of the list which is on the right-hand side of the expression will always come after the content which is on the left-hand side. We can reverse the order by writing the expression as follows to create a new list: list3 = list2+list1.
2) Using the append() Function to Join Lists
If we want to combine multiple lists, we can utilize a built-in list function called append(). Rather than combining all of the elements into one list, this function enables us to add objects to the end of an existing list.
Example Code:
list1 = ["Mehvish Ashiq", 1, 1, 23.34] list2 = [23.34, "Thomas Christopher"] list1.append(list2) print("List1: ", list1) print("List2: ", list2)
OUTPUT:
List1: ['Mehvish Ashiq', 1, 1, 23.34, [23.34, 'Thomas Christopher']] List2: [23.34, 'Thomas Christopher']
As depicted in the output, by using the append() function on list1, we are able to append a new list object to the end of list1 by passing list2 as a parameter.
3) Using a for Loop to Join Lists
Another way we can join lists is by using a for loop. A for loop gives us more control by allowing us to choose which elements we wish to add to an existing list. Below is a working code sample of how we would do so.
Example Code:
list1 = ["Mehvish Ashiq", 1, 1, 23.34] list2 = [23.34, "Thomas Christopher"] for element in list2: list1.append(element) print("List1: ", list1) print("List2: ", list2)
OUTPUT:
List1: ['Mehvish Ashiq', 1, 1, 23.34, 23.34, 'Thomas Christopher'] List2: [23.34, 'Thomas Christopher']
Here, the for loop iterates over the elements of the list2 and appends them one by one at the end of the list1.
4) Using the extend() Function to Join Lists
Similarly to the ‘+’ operator we can concatenate the content of one list with another by using the built-in list function extend(). The extend() function only accepts iterable objects as arguments and adds them to the end of the list. This means that passing a boolean value such as list1.extend(True) will generate an error because True is not an iterable object. Below is a working code sample of how we would use the extend() function to join lists.
Example Code:
list1 = ["Mehvish Ashiq", 1, 1, 23.34] list2 = [23.34, "Thomas Christopher"] list1.extend(list2) print("List1: ", list1) print("List2: ", list2)
OUTPUT:
List1: ['Mehvish Ashiq', 1, 1, 23.34, 23.34, 'Thomas Christopher'] List2: [23.34, 'Thomas Christopher']
As shown via the output of our code sample, the extend() function extends list1 by adding the elements of list2 to the end of list1.
5) Using Unpacking to Join Lists
Another useful tool that will expedite your path to becoming a well-rounded Python developer is the knowledge of unpacking. Unpacking is an operation that allows us to assign the content of iterable to a tuple or list by using the ‘*’ operator. Below is an example of how we can utilize unpacking to join lists.
Example Code:
list1 = ["Mehvish Ashiq", 1, 1, 23.34] list2 = [23.34, "Thomas Christopher"] list3 = [*list1, *list2] print("List1: ", list1) print("List2: ", list2) print("List3: ", list3)
OUTPUT:
List1: ['Mehvish Ashiq', 1, 1, 23.34] List2: [23.34, 'Thomas Christopher'] List3: ['Mehvish Ashiq', 1, 1, 23.34, 23.34, 'Thomas Christopher']
This approach was introduced in PEP 448 and is available in Python versions 3.5 and above. By using the ‘*’ operator, we unpack the content of list1 and list2 respectively into a new list we have called list3.The advantage of understanding unpacking in Python is that we can not only use it with lists but with any iterable object cleanly and concisely.
6) Using the zip() Function to Join Lists in Python
To make our list comprehensions more readable, we can utilize Pythons’ zip() function. The zip function accepts two iterable objects and returns an iterable tuple that contains the sequential pairs of each object. Below is a code sample of how we can use list comprehension in conjunction with Python's zip() function to join lists.
Example Code:
list1 = ["Mehvish Ashiq", 1, 1, 23.34] list2 = [23.34, "Thomas Christopher", 23.34] list3 = [x for y in zip(list1, list2) for x in y] print("List1: ", list1) print("List2: ", list2) print("List3: ", list3)
OUTPUT:
List1: ['Mehvish Ashiq', 1, 1, 23.34] List2: [23.34, 'Thomas Christopher', 23.34] List3: ['Mehvish Ashiq', 23.34, 1, 'Thomas Christopher', 1, 23.34]
Here, the zip() method generates an iterable tuple consisting of the pairs from list1 and list2 in sequential order. As a result, we can extract the content from the zip object by using list comprehension and storing them in a new list (list3).
7) Using NumPy Arrays to Join Lists
NumPy provides an alternative method to manipulating lists in python. In this example, we will learn how to use NumPy Arrays and one of its elementary methods concatenate(). As you may have guessed, the concatenate() method is a mirror of the ‘+’operator.
Example Code:
import numpy as np list1 = ["Mehvish Ashiq", 1, 1, 23.34] list2 = [23.34, "Thomas Christopher"] array1 = np.array(list1) array2 = np.array(list2) list3 = np.concatenate((array1, array2)) list3 = list(list3) print("List1: ", list1) print("List2: ", list2) print("List3: ", list3)
OUTPUT:
List1: ['Mehvish Ashiq', 1, 1, 23.34] List2: [23.34, 'Thomas Christopher'] List3: ['Mehvish Ashiq', '1', '1', '23.34', '23.34', 'Thomas Christopher']
To use this method, we first need to import NumPy. Next, we convert the lists into NumPy arrays by using the array() method that NumPy provides. Next, we use the concatenate() method to join both Numpy arrays. Lastly, we must use Pythons’ list() function to convert the NumPy Array back to a standard python list.
8) Using itertools to Join Lists in Python
Example Code:
import itertools as it list1 = ["Mehvish Ashiq", 1, 1, 23.34] list2 = [23.34, "Thomas Christopher"] list3 = it.chain(list1, list2) list3 = list(list3) print("List1: ", list1) print("List2: ", list2) print("List3: ", list3)
OUTPUT:
List1: ['Mehvish Ashiq', 1, 1, 23.34] List2: [23.34, 'Thomas Christopher'] List3: ['Mehvish Ashiq', 1, 1, 23.34, 23.34, 'Thomas Christopher']
We first must import the itertools module to use the chain() method. Next, we use the chain() method to chain/concatenate the arguments provided. Lastly, we must use Pythons’ list() function to convert the iterable returned by the chain() method back to a standard python list.
Joining Lists in Python by Removing Duplicate Values
In this section, we will be discussing how to join lists in Python by eliminating duplicate values.
1) Using a set() to Join Lists in Python
We can use one of Pythons’ built-in data-structures called a set() to remove duplicates when joining lists in Python. Below is an example of how we would do so.
Example Code:
list1 = ["Mehvish Ashiq", 1, 1, 23.34] list2 = [23.34, "Thomas Christopher"] set1 = set(list1) set2 = set(list2) set3 = set1.union(set2) list3 = list(set3) print("List1: ", list1) print("List2: ", list2) print("List3: ", list3)
OUTPUT:
List1: ['Mehvish Ashiq', 1, 1, 23.34] List2: [23.34, 'Thomas Christopher'] List3: [1, 23.34, 'Thomas Christopher', 'Mehvish Ashiq']
To use sets to join lists, we first have to convert the lists to by using Pythons’ set() method. Next, we create a new set(set3) which contains the union() of set1 and set2. Note that by using the union() function, duplicates are removed. Finally, we convert set3 to a list by using the list() method.
2) Using NumPy Arrays to Join Lists
In addition to the use case of NumPy we touched on in the previous section, NumPy also provides methods to join lists while eliminating duplicate values. By calling the
unique() method provided to us when working with NumPy Arrays, we are able to re-occurring elements.
import numpy as np list1 = [1, 2, 3, 4] list2 = [1, 4, 5, 6] result_list = list(np.unique(list1 + list2)) print(“result_list: ”, result_list)
OUTPUT:
result_list : [1, 2, 3, 4, 5, 6]
As depicted by the output, we can see that the duplicate values that exist when concatenating the list with the ‘+’ operator are removed. Those values being: 1 and 4.
Conclusion
In this tutorial, we learned about different ways to join/merge lists in Python considering whether we want to retain duplicate values or not. The approaches that we learned to join lists include various operators, functions, loops, modules, and data structures. Additionally, we learned how to use list unpacking and list comprehension to join lists. Although knowing how to perform list manipulation is important, it is also equally as important to know which operations would take more or less time to execute. Below are charts that benchmark the run-time of each method shown in this article using the timeit module. Each approach was executed one-million times.
Operators
Operator Name |
Article Use Case |
Timed Execution |
‘+’ Operator |
0.037 seconds |
|
‘*’ Operator |
0.025 seconds |
Built-in Functions
Function Name |
Article Use Case |
Timed Execution |
append() |
0.025 seconds |
|
extend() |
0.025 seconds |
|
zip() |
0.087 seconds |
Loops
Loop Name |
Article Use Case |
Timed Execution |
For Loop |
0.044 seconds |
Modules
Module Name |
Article Use Case |
Timed Execution |
NumPy.concatenate |
0.887 seconds |
|
NumPy.unique |
1.130 seconds |
|
itertools.chain |
0.036 seconds |
Data Structures
Data Structure Name |
Article Use Case |
Timed Execution |
set() |
0.117 seconds |
As a rule of thumb, using: operators, built-in functions and for-loops will yield the fastest result when joining lists. Though, there are times when the fastest approach may not provide a solution to the specific problem you are trying to solve. As a developer, it is important to be able to recognize these situations and adapt in order to implement a time-efficient approach.