How to Combine Two Lists in Python? (Adding Two Lists in Python)

When working with data in Python, you’ll often need to merge different sets of information into a single list. For instance, you might want to bring together customer details collected from multiple sources into one unified list.

In this guide, you will learn how to combine two lists in Python—using concatenation or the (+) operator, loops, and other operators. We’ll explore five common techniques in detail.

What is a Python List?

list in Python is a heterogeneous collection type that can hold different kinds of elements, such as numbers, strings, or even other objects. Unlike tuples, lists are mutable, meaning their contents can be updated—you can add new items, delete existing ones, or rearrange elements as needed.

Example:

my_list = [10, "apple", 3.5] # mixed types

print(my_list)

my_list.append("banana") # add item

my_list[0] = 20 # update list

del my_list[1] # delete item

print(my_list)

How to Combine Two Lists in Python? (5 Methods to Combine Lists in Python)

Python provides several techniques to join two lists together. Here are five commonly used methods:

  • Using the + operator or a list comprehension to create a new merged list.

  • Using a for loop or the extend() function to add items from one list to another.

  • Using the unpacking operator * to place all elements from both lists into a single list.

Each method has its own style of implementation, but the result is the same—you get a single list that contains elements from both input lists.

1. How to Combine Lists in Python with the + Operator?

The + operator isn’t limited to just numbers or strings—it can also be used to join two lists together. When applied to lists, it creates a new list that contains all the elements from both.

Let’s look at an example. Suppose we have one list with colors and another with shapes:

colors = ["red", "blue", "green"]

shapes = ["circle", "square", "triangle"]

# Combine the two lists

combined = colors + shapes

print(combined)

Output:

In the above example, the + operator produces a new list containing all the elements from both colors and shapes.

2. How to Combine Two Lists in Python with a for Loop

Another way to merge two lists is by looping through the elements of one list and appending them to the other. This approach modifies the first list directly instead of creating a new one.

For example, let’s take two lists—one with fruits and another with vegetables:

fruits = ["apple", "banana", "mango"]

vegetables = ["carrot", "broccoli", "spinach"]

# Append each vegetable to the fruits list

for item in vegetables:

fruits.append(item)

print(fruits)

Output:

In the above example, the for loop goes through each element in vegetables and appends it to fruits, resulting in one combined list.

3. How to Combine Two Lists in Python Using the extend() Method

The extend() method works similarly to a for loop, but is more concise. It takes another list (or any iterable) and appends each of its elements to the end of the first list.

Let’s try it with two lists—one containing countries and the other containing cities:

countries = ["USA", "Canada", "Germany"]

cities = ["New York", "Toronto", "Berlin"]

# Add all cities to the countries list

countries.extend(cities)

print(countries)

Output:

In the above example, extend() modifies the countries list by adding the items from cities, producing a single combined list.

4. Combining Lists with the * Unpacking Operator

In Python, the * operator can be used to unpack the contents of a list, meaning it inserts the individual elements of that list wherever it’s placed. This makes it a convenient way to merge multiple lists into one.

For example, consider two lists—one with animals and another with birds:

animals = ["dog", "cat", "rabbit"]

birds = ["sparrow", "eagle", "parrot"]

# Combine both lists using unpacking

combined = [*animals, *birds]

print(combined)

Output:

In the above code, *animals and *birds unpack their elements into a new list called combined, resulting in a single list with all items.

5. Merging Lists with List Comprehension

List comprehension in Python offers a compact way to build new lists by looping through existing ones. You can use it to flatten two or more lists into a single combined list.

For example, let’s combine a list of desserts with a list of drinks:

desserts = ["cake", "ice cream", "pie"]

drinks = ["coffee", "tea", "juice"]

# Combine both lists using list comprehension

combined = [item for group in [desserts, drinks] for item in group]

print(combined)

Output:

In the above example, the list comprehension iterates through each sublist (desserts and drinks) and unpacks its elements into a single flat list named combined.

Conclusion

Python provides several straightforward and flexible methods for combining lists. In this guide, we explored how to combine two lists in Python using five different techniques, each with its own advantages depending on the situation. Hopefully, this walkthrough gave you a clearer understanding of how list concatenation works in practice.

Experience the power of VPS hosting with the reliability of the cloud. BlueVPS.com offers a flexible, scalable, and fully customizable environment tailored to your needs. Enjoy dedicated resources, unlimited traffic, and seamless performance to keep your projects running smoothly. Get started today with BlueVPS and take your hosting to the next level!

Blog