Articles

How to Concatenate List in Python

Published Mar 20, 2025Updated Apr 11, 2025
Learn how to concatenate lists in Python using `+`, `*`, `for` loop, list comprehension, `extend()`, and `itertools.chain()`. Compare the methods for their efficiency.

Introduction to Python list concatenation

Imagine you’re planning a road trip with friends, and each person has their own playlist. Instead of switching between multiple playlists, you decide to combine them into one big playlist. This way, everyone’s favorite songs are in one place, making the journey smoother. Like merging playlists ensures a seamless experience, Python allows us to concatenate lists effortlessly.

In Python, list concatenation merges two or more lists into a single list. This operation is common in data processing, merging records, and organizing collections efficiently.

List concatenation has several advantages, as follows:

  • It helps combine related data, such as merging customer records from different sources.
  • It allows us to extend existing lists dynamically without modifying their original structure.
  • It simplifies data manipulation tasks, such as handling large datasets in machine learning or aggregating information from different lists.

Let’s explore the different ways to achieve this in Python.

Related Course

Learn Python 2

Learn the basics of the world's fastest growing and most popular programming language used by software engineers, analysts, data scientists, and machine learning engineers alike.Try it for free

Using the + operator to concatenate lists

The + operator is the most straightforward way to concatenate lists in Python. It merges two or more lists into a new list without modifying the original ones. An example to demonstrate this method is as follows:

list1 =[1,2,3]
list2 =[4,5,6]
combined = list1 + list2
print(combined)

The output generated by this example is:

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

The + operator is helpful, but it comes with both advantages and disadvantages:

Advantages

  • Easy to use: Intuitive syntax for quick list merging.
  • Preserves original lists: Ensures data integrity by not modifying the original list.

Disadvantages

  • Creates a new list: Increases memory usage by creating a new list.
  • Not ideal for large datasets: As this method creates a new list and copies elements from the original lists, it increases memory usage and can be inefficient for handling large datasets.

While the + operator is convenient, it may not be the best choice for handling large lists efficiently. Let’s explore alternative methods that optimize performance.

Using the * operator

The * operator (also known as the unpacking operator) allows us to extract elements from multiple lists and merge them into a new list.

Here’s an example showcasing this method:

a =[1,2,3]
b =[4,5,6]
# Use the unpacking operator to merge the lists
c =[*a,*b]
print(c)

The output for this code is:

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

This code uses the * operator to unpack and merge the elements of lists a and b into a new list, c.

While the * operator is concise, a for loop offers more control when custom logic is applied during concatenation.

Using a for loop

A for loop provides a manual way to concatenate lists. This approach is especially useful when additional logic needs to be applied to elements before adding them to the final list. Though not the most efficient, it offers flexibility in handling complex concatenation scenarios. Here’s an example of how a for loop can be used to concatenate two lists in Python:

list1 =[1,2,3]
list2 =[4,5,6]
for item in list2:
list1.append(item)
print(list1)

The output produced will be:

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

This code appends each element of list2 to list1, modifying list1 in place to achieve list concatenation without extra memory usage.

Using a for loop provides control over concatenation, allowing for element-specific modifications. However, it is less efficient than built-in methods due to multiple iterations and manual appending.

For a more concise approach, list comprehension simplifies this process while maintaining efficiency.

Using list comprehension for concatenation

List comprehension offers a concise and efficient way to merge multiple lists by flattening them into a single list using a one-liner expression. This approach eliminates the need for explicit loops, making the code cleaner and more readable. An example explaining this method is as follows:

list1 =[1,2,3]
list2 =[4,5,6]
merged_list =[item for lst in(list1, list2)for item in lst]
print(merged_list)

It produces an output as follows:

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

Here, the list comprehension iterates over each list in (list1, list2), then extracts each element (item) and adds it to merged_list. This results in a flattened list without needing a loop or additional methods.

While list comprehension creates a new list, what if we could merge lists without creating a new one? The extend() method lets us do just that!

Using the extend() method

The extend() method appends elements of one list to another, modifying the original list instead of creating a new one. Here’s an example demonstrating this method:

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

This is how the output for this code would look like:

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

This method is more memory-efficient than using + since it doesn’t create a new list. However, it alters the original list, which may not always be desirable. For a memory-efficient alternative that preserves the original lists, itertools.chain() is a great option.

Using itertools.chain()

itertools.chain() takes multiple iterables, such as lists, tuples, or other sequence types, and links them together into a single iterable.

Instead of creating a new list that stores all elements at once, itertools.chain() creates an iterator that retrieves elements one by one as needed, processing them on demand instead of loading them all into memory at once. This lazy execution makes it significantly more memory-efficient, especially for large datasets.

itertools.chain() is more memory efficient due to the following reasons:

  • No New List Creation: Unlike + or extend(), which create a new list in memory, itertools.chain() does not merge input iterables into a single object.
  • Lazy Execution:chain() keeps track of multiple iterables and yields elements one by one only when requested, reducing memory overhead.
  • Lower Memory Usage for Large Datasets: Ideal for handling large lists or streaming data, as it prevents unnecessary memory consumption by avoiding a full in-memory copy.

The syntax of itertools.chain() is as follows:

itertools.chain(iterable1, iterable2, ...) 

Parameters:

  • iterable1, iterable2, ...: Any number of iterable objects (lists, tuples, sets, generators, etc.) to be combined into a single iterator.

Return value:

  • Returns an iterator that yields elements from the input iterables sequentially.

This example demonstrates the use of itertools.chain():

from itertools import chain
list1 =[1,2,3]
list2 =[4,5,6]
result = chain(list1, list2)
print(list(result))

The output will be as follows:

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

This example shows how itertools.chain() merges list1 and list2 into an iterator, yielding elements one by one for efficiency. Converting it to a list gives [1, 2, 3, 4, 5, 6], but direct indexing isn’t possible without conversion.

Comparison of list concatenation methods

To understand which Python list concatenation method is best for your needs, let’s compare their efficiency, memory usage, and pros and cons.

MethodWorking MechanismTime ComplexitySpace ComplexityProsCons
+ operatorCreates a new list by copying elements from both lists.O(n + m)O(n + m)Simple and readableCreates a new list, increasing memory usage
* operatorUses unpacking to insert elements of lists into a new listO(n + m)O(n + m)More flexible than +, can unpack multiple listsCreates a new list, consuming extra memory
for loopIterates through elements and appends them to a listO(n + m)O(n + m)Provides flexibility for additional processingLess efficient, requires manual appends
List comprehensionIterates over elements and constructs a new list in a single lineO(n + m)O(n + m)More efficient than a for loop, concise syntaxCreates a new list, consuming extra memory
extend() methodModifies an existing list by adding elements from another listO(m)O(1)Efficient, does not create a new listAlters the original list, which may not be desirable
itertools.chain()Creates an iterator that retrieves elements lazily but does not create a new listO(n + m)O(1)Memory-efficient, ideal for large datasetsReturns an iterator, not a list, requiring conversion if needed

Conclusion

List concatenation in Python can be achieved through various methods, each with distinct benefits. The + operator creates a new list, while * is used for unpacking. for loops offer control but can be inefficient, whereas list comprehension provides a cleaner approach. The extend() method modifies lists in place, and itertools.chain() is memory-efficient for large datasets.

To further enhance your understanding of Python and explore more advanced techniques, check out Codecademy’s Learn Intermediate Python 3 course, where you’ll build on your skills with more hands-on coding exercises.

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team
close