Python
concatenate lists
Python lists
programming
data manipulation

How do I concatenate two lists in Python?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

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:

python
1list1 = [1, 2, 3]
2list2 = [4, 5, 6]
3result = list1 + list2
4print(result)  # Output: [1, 2, 3, 4, 5, 6]

Technical Explanation:

  • The + operator creates a new list by appending the contents of list2 to the end of list1.
  • 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:

python
1list1 = [1, 2, 3]
2list2 = [4, 5, 6]
3list1.extend(list2)
4print(list1)  # Output: [1, 2, 3, 4, 5, 6]

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:

python
1list1 = [1, 2, 3]
2list2 = [4, 5, 6]
3result = [x for x in list1] + [y for y in list2]
4print(result)  # Output: [1, 2, 3, 4, 5, 6]

Technical Explanation:

  • This involves iterating over each list and constructing a new list.
  • While list comprehension allows 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:

python
1list1 = [1, 2, 3]
2list2 = [4, 5, 6]
3result = [*list1, *list2]
4print(result)  # Output: [1, 2, 3, 4, 5, 6]

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 O(n)O(n) operation where n is 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.

python
1tuple1 = (1, 2, 3)
2tuple2 = (4, 5, 6)
3result = tuple1 + tuple2
4print(result)  # Output: (1, 2, 3, 4, 5, 6)

Summary Table

MethodIn-PlaceMutableSyntaxPython VersionUse Case
+ OperatorNoYeslist1 + list2AllSimple concatenation, no mutation of original lists
extend()YesYeslist1.extend(list2)AllWhen you need to modify the existing list
List ComprehensionNoYes[x for x in list1] + [y for y in list2]AllAdding logic during concatenation
* OperatorNoYes[*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.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms