How to combine lists in Python?


There are a few different ways to combine lists in Python, and the method you’ll want to use depends on your specific needs. Let’s take a look at a few of the most common methods for combining lists.

The first method we’ll cover is the .extend() method.

This method modifies an existing list by adding the elements of another list to it. The syntax for this method is as follows:

list1.extend(list2)
Code language: CSS (css)

This code will add the elements of list2 to list1. Keep in mind that when using the .extend() method, the original list (list1) is modified in-place, meaning no new object is created.

The second method we’ll look at is the .append() method.

One way to combine lists in Python is to use the append method. The append method adds an element to the end of a list. It takes one argument, which is the element to be added. Let’s see how it works with a simple example:

>>> my_list = [1, 2, 3] >>> my_list.append(4) >>> my_list [1, 2, 3, 4]

As you can see from the example above, we start with a list that contains three elements. We then use the append method to add a fourth element to the end of the list. When we print out the list, we see that it now contains four elements.

We can also use the append method to add another list to an existing list:

>>> my_list = [1, 2, 3] >>> my_list.append([4, 5]) >>> my_list [1, 2, 3, [4, 5]]

The third and final method we’ll cover is the + operator.

This operator combines two lists into a new object without modifying either of the original lists. The syntax for this operator is as follows:

list1 + list2

This code will create a new object that contains all of the elements from both list1 and list2—the original lists are not modified at all.

How to combine lists in Python using the built-in zip() function

Suppose we have two lists, list1, and list2, that we want to combine into a single list. We can do this using the built-in zip() function like this:

list1 = [1, 2, 3] list2 = [4, 5, 6] zipped_list = zip(list1, list2) print(zipped_list) # [(1, 4), (2, 5), (3, 6)]
Code language: PHP (php)

Notice that the result of zip() is a list of tuples. Each tuple contains one element from each of the input lists. If the input lists are of different lengths, then zip() will stop after it reaches the end of the shortest list.

We can also unzip a list of tuples into two separate lists using the built-in zip() function. We do this by passing zip() a list of tuples and assigning the result to two separate variables like this:

zipped_list = [(1, 4), (2, 5), (3, 6)] list1, list2 = zip(*zipped_list) print(list1) # (1, 2, 3) print(list2) # (4, 5 ,6)
Code language: PHP (php)

The * in front of zipped_list unpacks the list of tuples into individual elements. This is necessary because zip() expects its arguments to be individual elements—not a list of tuples.

Combining lists in Python is a process you should feel confident about after reading this blog post! Using either the .extend(), .append(), + operator, or using the built-in zip() function, you can easily combine two or more lists into one without any trouble at all. Remember, each method has its own specific use case, so choose wisely!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts