How to Concatenate Two Lists in Python?

How to Concatenate Two Lists in Python?

You can use the ‘+’ operator to concatenate two lists in Python.

Concatenating lists is a common task in Python, especially when working with data collections. Python provides multiple ways to concatenate lists. In this blog, let’s explore the different methods to concatenate lists with the use cases. 

Table of Contents:

Methods of Concatenating Two Lists in Python

Method 1: Using ‘+’ Operator to Concatenate Two Lists in Python

The ‘+’ operator is the simplest method to concatenate two lists. When you use this operator it combines the elements of the two lists and forms a new list.

Example: 

Python

Output:

Explanation: 

The + operator creates a new list by combining the elements from list1 and list2 in the order they appear.

Method 2: Using extend() Method to Concatenate Two Lists in Python

The extend() method allows you to add the elements of one list to the end of another list. It modifies the original list, which means no new list is created.

Example:

Python

Output:

Explanation: 

The extend() method adds each element from list2 to list1, modifying list1 directly.

Method 3: Using * Operator to Concatenate Two Lists in Python

The ‘*’ operator helps in unpacking the element from multiple lists and combines the lists forming a new list.

Example:

Python

Output:

Explanation: 

Using * to unpack the elements of list1 and list2 forming a new list.

Method 4: Using a For Loop to Concatenate Two Lists in Python

Two lists can be concatenated by using the looping statements like ‘for loop’ which appends to the list and modifies the original list without forming a new list.

Example:

Python

Output:

Explanation: 

The loop iterates over list2, adding each element to list1 one by one.

Method 5: Using itertools.chain() to Concatenate Two Lists in Python

The itertools.chain() function is an efficient method to concatenate lists in Python. This method does not create a new list but returns an iterator.

Example:

Python

Output:

Explanation: 

The itertools.chain() function takes multiple iterables and returns their values consecutively, making it a memory-efficient way to concatenate lists.

Method 6: Using List Comprehension to Concatenate Two Lists in Python

List comprehension is another efficient method to concatenate two lists by creating a new list that includes elements from both lists. It’s an alternative to using a loop for concatenation.

Example:

Python

Output:

Explanation: 

This method uses list comprehension to collect the elements of both list1 and list2 into a new list. It’s essentially a more readable alternative to the + operator.

Conclusion

Python offers several ways to concatenate lists, each with its use case. The + operator is the simplest, while methods like extend() and itertools.chain() offer efficiency in certain situations. You can choose the specific methods based on the requirement.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner