How do I concatenate two lists in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Concatenating two lists in Python is a fundamental operation that is frequently needed when dealing with collections of data. This process involves combining elements from two lists into a single list, and Python provides several methods to achieve this. Each method has its applications across various scenarios. Let's dive deeper into these methods and the technicalities behind them.
List Concatenation Methods
1. Using the + Operator
The simplest way to concatenate two lists in Python is by using the + operator. This method creates a new list that contains elements from the two lists in the order they appear.
Example:
Technical Explanation:
- The
+operator creates a new list by appending the contents oflist2to the end oflist1. - It does not modify the original lists, which can be important when maintaining immutability is necessary.
2. Using the extend() Method
The extend() method modifies the first list in place by adding elements from the second list.
Example:
Technical Explanation:
- The
extend()method iterates over the elements of the second list, appending each to the first list. - This method modifies the original list (
list1) directly.
3. Using List Comprehension
List comprehension provides a concise way to concatenate lists that might involve conditions or transformations.
Example:
Technical Explanation:
- This involves iterating over each list and constructing a new list.
- While
list comprehensionallows filtering or mapping, the above example achieves straightforward concatenation.
4. Using the * Operator (Python 3.5+)
Introduced in Python 3.5, using * (unpacking) can also be employed to concatenate lists within a list literal.
Example:
Technical Explanation:
- The
*operator unpacks the lists into a new list. - This method is non-destructive to the original lists and is suited for dynamic programming situations.
Additional Details and Considerations
Performance Considerations
- In-Place versus Out-of-Place: Methods like
extend()modify the original list, making them in-place operations, while using+creates a new list, making it out-of-place. - Complexity: Generally, concatenating lists is an operation where
nis the total number of elements in both lists.
Immutable Collections
For scenarios that require immutable collections, the tuple data structure can be used, albeit with slightly different syntax.
Summary Table
| Method | In-Place | Mutable | Syntax | Python Version | Use Case |
+ Operator | No | Yes | list1 + list2 | All | Simple concatenation, no mutation of original lists |
extend() | Yes | Yes | list1.extend(list2) | All | When you need to modify the existing list |
| List Comprehension | No | Yes | [x for x in list1] + [y for y in list2] | All | Adding logic during concatenation |
* Operator | No | Yes | [*list1, *list2] | 3.5+ | When unpacking is needed |
Each method has its use cases and scenarios where it fits best. Understanding these will help Python developers make precise, efficient coding decisions tailored to their program’s needs.

