When working with lists in Python, combining multiple related data sets into a single list is often necessary—for example, consolidating customer records from different sources into one master list. This tutorial will show how to combine two lists in Python using various methods, such as concatenation, loops, and other operators; I'll cover five specific techniques.
What is a list in Python?
A list in Python or a Python list is a heterogeneous data type that contains a collection of elements including strings, integers, and characters. Unlike tuples, Python lists are mutable, implying that they can be manipulated or modified. You can perform operations such as adding, removing, and reordering elements.
How to combine two lists in Python: 5 Methods
There are various ways to combine two [Python lists](How to Reverse a List in Python). Below, I will show you five different methods to combine two lists in Python: using the + operator or list comprehension to produce a new concatenated list; a for loop or extend() method to append elements of one list to the other; and using * operators to unpack elements into a combined list.
Each approach has its syntax and constructs the combined list slightly differently, but all allow merging the elements of two lists into a single output list.
Method 1. Python: Combine lists using the (+) operator
The +
operator is an operator that is used to add two or more integers or floating data types. It is also used to concatenate strings and can also be used to combine or concatenate lists.
Consider the two lists below. The first list contains integer values while the second contains string values.
list_1 = [10, 20, 30, 40]
list_2 = ['Alice', 'Mike', 'Bob']
To concatenate the two lists, use the +
operator. The code below combines the two strings and stores the result in a new list called combined_list
.
combined_list = [list_1 + list_2]
You can confirm the contents of the new list using the print
statement.
print(combined_list)
Output
[10, 20, 30, 40, 'Alice', 'Mike', 'Bob']
Method 2. Python: Combine lists using a for loop
Alternatively, you can use a for loop
to add the elements of one string to another. Using the same Python lists in the previous example, you can use a for loop
to append the elements of list_2
to list_1
as follows.
for item in list_2:
list_1.append(item)
You can confirm the changes to the list using the print statement below.
print(list_1)
Output
[10, 20, 30, 40, 'Alice', 'Mike', 'Bob']
Method 3. Python: Combine lists using the extend() function
Just like the for loop
, the extend()
function adds the elements of one list to another. In the code below, the extend()
function adds the contents of list_2
to list_1
.
list_1.extend(list_2)
You can confirm using the print statement as demonstrated.
print(list_1)
Output
[10, 20, 30, 40, 'Alice', 'Mike', 'Bob']
*
operator
Method 4. Python: Combine lists using theWhen it precedes a list name, Python’s *
operator simply unpacks the items contained in the list. For example, the statement *list_1
replaces the list with its elements at index positions.
To combine the two lists in Python, list_1
and list_2
, use the following statement:
combined_list = [ *list_1, *list_2]
The above statement unpacks all the elements contained in both lists and stores them in a new list called combined_list
.
Output
[10, 20, 30, 40, 'Alice', 'Mike', 'Bob']
Method 5. Python: Combine lists using list comprehension
In Python, list comprehension provides a shorter syntax to create a new list based on the elements of an existing list. The following list comprehension loops through both lists and stores individual elements in a new list named combined_list
combined_list = [ x for y in [list_1, list_2] for x in y]
Output
[10, 20, 30, 40, 'Alice', 'Mike', 'Bob']
Conclusion
You can use multiple ways of combining or concatenating strings in Python. In this tutorial, I’ve shown how to combine two lists in Python using five different methods. I hope you found this guide insightful. Your feedback is very welcome.
If this guide was useful, you might also want to check out our tutorials on how to manage conda environments and how to install anaconda on Ubuntu.