How to combine two lists in Python

Introduction

As you learn programming, you'll come across various data structures, and one of the most common and versatile data structures is a list. In Python, lists are used to store a collection of items, which can be of different types like integers, strings, or even other lists.

In this blog, we'll explore how to combine two lists in Python. Combining lists is a common operation, and knowing how to do this effectively will enhance your programming skills. We'll go through different methods and provide examples to help you understand the concept better.

Before diving into the methods, let's first understand what combining lists means. Imagine you have two baskets of fruits - one with apples and the other with oranges. If you want to put all the fruits in a single basket, you'll combine the two baskets. Similarly, in Python, we can combine two lists to create a new list containing elements from both the original lists.

Method 1: Using the `+` Operator

The simplest way to combine two lists in Python is by using the `+` operator. This operator can be used to concatenate (join) two lists together, creating a new list that contains all the elements from both lists.

Here's an example:

``````list1 = [1, 2, 3]
list2 = [4, 5, 6]

combined_list = list1 + list2
print(combined_list)
``````

Output:

``````[1, 2, 3, 4, 5, 6]
``````

In this example, we have two lists `list1` and `list2`, and we want to combine them into a new list called `combined_list`. The `+` operator joins the two lists, and the result is the combined list.

It's important to note that the `+` operator doesn't modify the original lists. Instead, it creates a new list that contains the combined elements.

Method 2: Using the `extend()` Method

Another way to combine two lists is by using the `extend()` method. This method extends the first list by adding all the elements from the second list to it. Unlike the `+` operator, this method modifies the original list.

Here's an example:

``````list1 = [1, 2, 3]
list2 = [4, 5, 6]

list1.extend(list2)
print(list1)
``````

Output:

``````[1, 2, 3, 4, 5, 6]
``````

In this example, we use the `extend()` method to add the elements from `list2` to `list1`. After the operation, `list1` contains the combined elements, while `list2` remains unchanged.

Method 3: Using List Comprehension

List comprehension is a concise way to create a new list by iterating over the elements of one or more existing lists. You can use list comprehension to combine two lists by iterating over the elements of both lists and adding them to a new list.

Here's an example:

``````list1 = [1, 2, 3]
list2 = [4, 5, 6]

combined_list = [x for x in list1] + [x for x in list2]
print(combined_list)
``````

Output:

``````[1, 2, 3, 4, 5, 6]
``````

In this example, we use list comprehension to create two new lists that contain the elements of `list1` and `list2`, respectively. Then, we use the `+` operator to combine these new lists into the `combined_list`.

While this method might not be as straightforward as the previous methods, it can be useful in situations where you want to apply some condition or transformation to the elements before combining them.

For example, let's say you want to combine only the even numbers from both lists. You can achieve this using list comprehension as follows:

``````list1 = [1, 2, 3]
list2 = [4, 5, 6]

combined_list = [x for x in list1 if x % 2 == 0] + [x for x in list2 if x % 2 == 0]
print(combined_list)
``````

Output:

``````[2, 4, 6]
``````

In this example, we use a conditional statement `if x % 2 == 0` to filter out only the even numbers from both lists before combining them.

Method 4: Using the `append()` Method in a Loop

Another way to combine two lists is by using the `append()` method in a loop. This method allows you to add elements from the second list to the first list one by one. Like the `extend()` method, this method also modifies the original list.

Here's an example:

``````list1 = [1, 2, 3]
list2 = [4, 5, 6]

for x in list2:
list1.append(x)

print(list1)
``````

Output:

``````[1, 2, 3, 4, 5, 6]
``````

In this example, we use a `for` loop to iterate over the elements of `list2`. Inside the loop, we use the `append()` method to add each element from `list2` to `list1`. After the loop, `list1` contains the combined elements, while `list2` remains unchanged.

This method can also be useful when you want to apply some condition or transformation to the elements before combining them.

For example, let's say you want to combine only the odd numbers from both lists. You can achieve this using the `append()` method in a loop as follows:

``````list1 = [1, 2, 3]
list2 = [4, 5, 6]

for x in list2:
if x % 2 != 0:
list1.append(x)

print(list1)
``````

Output:

``````[1, 2, 3, 5]
``````

In this example, we use a conditional statement `if x % 2 != 0` to filter out only the odd numbers from `list2` before appending them to `list1`.

Conclusion

In this blog, we've seen four different methods to combine two lists in Python:

1. Using the `+` operator
2. Using the `extend()` method
3. Using list comprehension
4. Using the `append()` method in a loop

Each method has its own advantages and use cases, so it's essential to understand them and choose the right one based on your specific requirements. By learning these methods, you'll be better equipped to manipulate and process lists in your Python programs effectively.

Learn to code in our 100% online programs

Altcademy coding bootcamp offers beginner-friendly, online programs designed by industry experts to help you become a coder. 85%+ of Altcademy alumni are hired within 6 months after graduation. See how we teach, or click on one of the following programs to find out more.