How to Concatenate List in Python
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.
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 freeUsing 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 + list2print(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 listsc =[*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
+
orextend()
, 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 chainlist1 =[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.
Method | Working Mechanism | Time Complexity | Space Complexity | Pros | Cons |
---|---|---|---|---|---|
+ operator | Creates a new list by copying elements from both lists. | O(n + m) | O(n + m) | Simple and readable | Creates a new list, increasing memory usage |
* operator | Uses unpacking to insert elements of lists into a new list | O(n + m) | O(n + m) | More flexible than + , can unpack multiple lists | Creates a new list, consuming extra memory |
for loop | Iterates through elements and appends them to a list | O(n + m) | O(n + m) | Provides flexibility for additional processing | Less efficient, requires manual appends |
List comprehension | Iterates over elements and constructs a new list in a single line | O(n + m) | O(n + m) | More efficient than a for loop, concise syntax | Creates a new list, consuming extra memory |
extend() method | Modifies an existing list by adding elements from another list | O(m) | O(1) | Efficient, does not create a new list | Alters the original list, which may not be desirable |
itertools.chain() | Creates an iterator that retrieves elements lazily but does not create a new list | O(n + m) | O(1) | Memory-efficient, ideal for large datasets | Returns 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.
'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 teamRelated articles
- Article
How to Reverse a List in Python
Learn how to reverse a list in Python using `.reverse()`, `reversed()`, slicing, the two-pointer method, loops, and recursion with examples. - Article
List Comprehensions
Learn about how to create new lists in one line of Python! - Article
How to Sort Lists of Lists in Python with Examples
Learn how to sort lists of lists in Python. Explore simple to advanced methods, including, `sorted()`, `map()`, and multi-criteria sorting for complex data.
Learn more on Codecademy
- Free 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.Beginner Friendly17 hours - Skill path
Pass the Technical Interview with Python
Learn about the computer science concepts of data structures and algorithms and build implementations of each from scratch in modern Python.Includes 8 CoursesWith CertificateIntermediate25 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours