How Do I Concatenate Two Lists in Python

Lists are a fundamental data structure in Python and play a critical role in many applications.

At times, you may want to combine two or more lists into a single list.

This is known as concatenating lists.

In this article, we’ll explore the various ways of concatenating lists in Python and discuss the pros and cons of each method.


Method 1: Using the + operator

The most straightforward way to concatenate two lists is to use the + operator. The syntax is as follows:

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

list3 = list1 + list2

print(list3)

Output:

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

The + operator takes two lists as operands and returns a new list that consists of the elements of both lists in the order they were specified.

This method is simple and easy to understand, but it has some limitations.

For example, if you have a large number of lists that you need to concatenate, you’ll have to use multiple + operators, which can quickly become cumbersome and difficult to maintain.

Method 2: Using the extend() method

Another way to concatenate lists is to use the extend() method.

The extend() method is a built-in method that can be called on any list object to add the elements of another list to the end of the list.

The syntax is as follows:

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

list1.extend(list2)

print(list1)

Output:

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

The extend() method is more efficient than the + operator because it modifies the existing list in-place instead of creating a new one.

This can be especially useful when you’re working with large lists and need to concatenate them frequently.

However, the extend() method also has some disadvantages.

For example, if you want to concatenate two lists into a new list without modifying either of the original lists, the extend() method is not suitable.

Method 3: Using the append() method

Another option is to use the append() method to concatenate lists. The append() method adds a single element to the end of a list.

To concatenate lists using the append() method, you’ll need to iterate over the elements of one of the lists and use the append() method to add each element to the other list.

The syntax is as follows:

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

for i in list2:
    list1.append(i)

print(list1)

Output:

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

This method is less efficient than the extend() method because it requires you to loop through the elements of one of the lists and add them to the other list one at a time. However, it can be useful when you need to modify one of the original lists and keep the other unchanged.

Method 4: Using the itertools.chain() method

The itertools module in Python provides a chain() method that can be used to concatenate lists. The chain() method takes any number of iterables as arguments and returns a single iterable that contains all the elements of the input iterables in the order they were specified. The syntax is as follows:

import itertools

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

list3 = list(itertools.chain(list1, list2))

print(list3)

Output:

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

The chain() method is more efficient than the + operator and the extend() method when concatenating a large number of lists because it avoids the creation of intermediate lists.

However, it requires you to import the itertools module, which can make your code less readable.


Conclusion

In this article, we’ve explored the various ways of concatenating lists in Python.

Whether you use the + operator, the extend() method, the append() method, or the itertools.chain() method will depend on your specific use case and the requirements of your application.

By understanding the pros and cons of each method, you can make an informed decision and choose the best approach for your needs.