5 Ways to Combine Lists in Python: A Beginner’s Guide

目次

1. Introduction

Python is a highly popular programming language known for its simple, easy-to-read syntax and extensive libraries. Among its features, the list is one of the most frequently used data types for managing and manipulating data. Lists are a convenient structure that lets you handle multiple pieces of data together, making them indispensable in Python programming. In particular, combining multiple lists into a single list — “list concatenation” — is one of the fundamental operations for improving data manipulation efficiency. Properly understanding this operation can greatly increase the speed of data processing and simplify your code. This article provides a detailed explanation of methods for concatenating lists in Python. To make it accessible for beginners, we cover everything from basic techniques to more advanced tricks, as well as performance differences and important caveats. We will carefully explain, with concrete code examples, how to choose the best concatenation method for your needs, so please read through to the end. In the next section, we’ll take a closer look at the five main ways to concatenate lists in Python.

2. Five main ways to combine lists

Python provides several ways to combine lists. Each method has its own characteristics, usage, and suitable situations. In this section, we explain the five main methods used to combine lists, along with concrete code examples.

2.1 append() method

Features

append() method is the basic way to add a single element to the end of a list. This method modifies the list in place, so the original list is updated.

Usage example

The code below uses append() to add a new element to a list.
list1 = [1, 2, 3]
list1.append(4)
print(list1)  ## Output: [1, 2, 3, 4]
However, note that adding an entire list will create a nested list.
list1 = [1, 2, 3]
list1.append([4, 5])
print(list1)  ## Output: [1, 2, 3, [4, 5]]

Notes

append() is suitable for adding a single element, but if you want to combine entire lists it is more effective to use other methods.

2.2 extend() method

Features

The extend() method is used to extend a list by adding elements from another list or iterable (such as a tuple or set) to the end. It’s useful when you want to combine lists without nesting.

Usage example

The code below uses extend() to join two lists.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1)  ## Output: [1, 2, 3, 4, 5, 6]

Notes

  • extend() modifies the original list directly.
  • If you want to combine nested lists, you’ll need a different approach.

2.3 insert() method

Features

The insert() method is used to insert an element at a specific position in a list. It’s useful when insertion, rather than concatenation, is the goal.

Usage example

The code below uses insert() to insert an element at a specific position in a list.
list1 = [1, 2, 4]
list1.insert(2, 3)
print(list1)  ## Output: [1, 2, 3, 4]

Notes

Frequent use of insert() can decrease performance when the list has many elements.

2.4 + operator

Features

The + operator combines two lists to create a new list. The original lists are not modified; a new list is returned.

Usage example

The code below uses the + operator to concatenate lists.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2
print(combined)  ## Output: [1, 2, 3, 4, 5, 6]

Notes

  • Because it creates a new list, memory usage can increase.
  • The original lists are not modified.

2.5 Using slices

Features

Using slices allows you to insert or combine elements at any position in a list. It’s a highly flexible method.

Usage example

The code below uses slicing to combine lists.
list1 = [1, 2, 5]
list1[2:2] = [3, 4]
print(list1)  ## Output: [1, 2, 3, 4, 5]

Notes

When using slicing, precise index specification is required. Incorrect indices can lead to unexpected results.
侍エンジニア塾

3. Recommended Methods for Combining Lists by Use Case

There are various options for combining lists, and it’s important to choose the right method depending on the specific use or situation. This section explains in detail which method to choose based on common scenarios.

3.1 Combining small lists

Recommended method: + operator

For combining small lists, the + operator is the most concise and efficient. Because this method creates a new list, it’s also suitable when you don’t want to modify the original lists.

Example

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

Advantages

  • Readable syntax.
  • Good performance for small datasets.

Notes

  • Creates a new list, so it’s not suitable for large datasets.

3.2 Combining large datasets

Recommended method: extend() method

When combining large amounts of data, the extend() method is efficient. It updates the original list in place, reducing memory usage.

Example

list1 = [i for i in range(100000)]
list2 = [j for j in range(100000)]
list1.extend(list2)
print(len(list1))  ## Output: 200000

Advantages

  • Suitable for processing large datasets.
  • Memory-efficient.

Notes

  • Not suitable if you want to avoid modifying the original list.

3.3 Inserting elements at a specific position

Recommended: insert() method or slicing

To insert elements at a specific position, use the insert() method or slicing. This is useful when the insertion point is known.

Example (insert)

list1 = [1, 2, 4]
list1.insert(2, 3)
print(list1)  ## Output: [1, 2, 3, 4]

Example (slice)

list1 = [1, 2, 5]
list1[2:2] = [3, 4]
print(list1)  ## Output: [1, 2, 3, 4, 5]

Advantages

  • Allows insertion at any position.
  • Using slicing lets you insert multiple elements at once.

Notes

  • Processing speed may decrease for lists with many elements.

3.4 When adding elements repeatedly

Recommended method: append() method

When adding elements one by one inside a loop, append() is optimal. It efficiently appends single elements to a list.

Example

list1 = []
for i in range(5):
    list1.append(i)
print(list1)  ## Output: [0, 1, 2, 3, 4]

Advantages

  • Simple and easy to understand.
  • Suitable when processing elements one at a time.

Notes

  • Not suitable for combining entire lists.

3.5 Combining multiple methods

Combining multiple methods enables more flexible data manipulation. For example, you can combine slicing with extend() to efficiently add multiple elements at a specific position.

Example

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

Summary

  • Small datasets: The + operator is simple and recommended.
  • Large datasets: extend() is appropriate when considering memory efficiency.
  • Adding elements one at a time: append() is optimal.
  • Inserting at a specific position: Use insert() or slicing.
  • Adding multiple elements at a specific position: Slicing is flexible and effective.

4. Performance Comparison

In Python, performance can vary depending on how lists are joined. When the amount of data is small, performance differences are usually negligible, but as the data size increases the differences can become noticeable. In this section, we compare the performance of common join methods.

4.1 Test Method and Assumptions

To compare performance, the code is run under the following conditions.
  • List size: Using lists of 100,000 elements.
  • Comparison metric: Measure execution speed (processing time).
  • Environment: Measurements are taken using the Python standard library’s time module.
Based on the code below, we compare the execution speed of the + operator, the extend() method, the append() method, and slicing.

Sample code

import time

## Test lists
list1 = [i for i in range(100000)]
list2 = [j for j in range(100000)]

## + operator
start = time.time()
combined = list1 + list2
print("Plus:", time.time() - start)

## extend() method
start = time.time()
list1.extend(list2)
print("Extend:", time.time() - start)

## append() method (adding in a loop)
list1 = [i for i in range(100000)]
start = time.time()
for item in list2:
    list1.append(item)
print("Append in Loop:", time.time() - start)

## Slice
list1 = [i for i in range(100000)]
start = time.time()
list1[len(list1):] = list2
print("Slice:", time.time() - start)

4.2 Performance Results

An example of the execution results is shown below (results may vary depending on environment). | Method | Execution time (s) | |–|| | + operator | 0.08 | | extend() | 0.03 | | append() (loop) | 0.25 | | Slicing | 0.04 |

4.3 Explanation of Results

+ operator

  • Speed: Moderate.
  • Characteristics: Creates a new list, so memory usage may increase.
  • Use case: Suitable for joining small lists.

extend() method

  • Speed: Very fast.
  • Characteristics: Modifies the original list directly, so it is memory-efficient.
  • Use case: Ideal for joining large amounts of data.

append() method (using a loop)

  • Speed: Very slow.
  • Characteristics: Adds elements one by one, which slows processing.
  • Use case: Suitable when data is generated one element at a time, but not recommended for large datasets.

Slicing

  • Speed: Fast.
  • Characteristics: Highly flexible and useful for merging or inserting elements at specific positions.
  • Use case: Effective when you want to insert multiple elements at a specific position.

4.4 Which Method Should You Choose?

Small lists:

  • Recommended method: + operator or extend().
  • Reason: Memory usage is not a major concern and the syntax is concise.

Large lists:

  • Recommended method: extend().
  • Reason: Because it is fast and memory-efficient.

Adding elements at a specific position:

  • Recommended method: Slicing.
  • Reason: It is flexible and can efficiently insert multiple elements.
RUNTEQ(ランテック)|超実戦型エンジニア育成スクール

5. Precautions and Common Errors When Combining Lists

Combining lists is a useful operation, but if used improperly it can lead to unintended results. This section explains points to watch out for when combining lists, common errors, and how to handle them.

5.1 Confusing append() and extend()

Problem

Because append() and extend() have similar names, they are often confused. This is especially common when you intend to add an entire list.

Example

list1 = [1, 2, 3]
list2 = [4, 5]

list1.append(list2)
print(list1)  ## Output: [1, 2, 3, [4, 5]]
In this case, the entire list2 is added as a nested list.

Solution

If you want to expand and add all the elements of a list, use extend().
list1 = [1, 2, 3]
list2 = [4, 5]

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

5.2 Memory Usage of the + Operator

Problem

When you use the + operator, a new list is created. As a result, combining very large lists can increase memory usage.

Example

list1 = [i for i in range(1000000)]
list2 = [j for j in range(1000000)]

combined = list1 + list2  ## A new list is created
In this operation, a new list named combined is created in memory in addition to list1 and list2, resulting in high memory load.

Solution

When working with large datasets, using extend() can improve memory efficiency.

5.3 Index Errors with insert() and Slicing

Problem

When using insert() or slicing, specifying an invalid index may produce unexpected results.

Example

list1 = [1, 2, 3]
list1.insert(10, 4)
print(list1)  ## Output: [1, 2, 3, 4]
In this example, even though index 10 does not exist, no error occurs and the element is simply added to the end.

Solution

Check the length of the list and specify an appropriate index, or consider using slicing instead.

5.4 Side Effects of Mutable Lists

Problem

append() and extend() modify the original list directly. Therefore, if the same list is referenced in multiple places, unexpected side effects may occur.

Example

list1 = [1, 2, 3]
list2 = list1  ## list1 and list2 reference the same list
list1.append(4)
print(list2)  ## Output: [1, 2, 3, 4]

Solution

Copy the list to prevent the original list from being changed.
list1 = [1, 2, 3]
list2 = list1[:]
list1.append(4)
print(list2)  ## Output: [1, 2, 3]

5.5 Data Type Mismatches

Problem

If the data you try to combine into a list is not of the expected type, an error may occur.

Example

list1 = [1, 2, 3]
list1.extend(4)  ## TypeError: 'int' object is not iterable

Solution

Ensure that the data you are adding to a list is iterable (such as a list or tuple).
list1 = [1, 2, 3]
list1.extend([4])  ## Correct method

5.6 Summary

  • Understand the difference between append() and extend(), and use them appropriately.
  • Consider memory consumption and determine when to prefer extend() over the + operator.
  • When using slices or insert(), pay attention to indexes to ensure elements are added where intended.
  • Copy lists to prevent side effects — don’t forget this step.
  • To avoid unexpected errors when combining lists, check the input data types in advance.

6. Summary

Merging lists in Python is an essential skill for efficiently managing and manipulating data. In this article, we explained in detail the five main ways to join lists, their use cases and performance characteristics, and important caveats. In this section, we’ll review what we’ve covered and reaffirm how to choose the right list-joining method.

6.1 Recap of method characteristics

1. append() method

  • Feature: Adds a single element to the end of the list.
  • Use case: Ideal when you want to add elements one at a time.
  • Caveat: Not suitable for merging entire lists.

2. extend() method

  • Feature: Expands another list or iterable and appends its items to the end of the original list.
  • Use case: When you want to efficiently join large amounts of data.
  • Caveat: Modifies the original list.

3. insert() method

  • Feature: Inserts a single element at a specific position.
  • Use case: When you need to add an element at a specific position in the list.
  • Caveat: Frequent use may degrade performance.

4. + operator

  • Feature: Combines two lists to create a new list.
  • Use case: Convenient for combining small lists.
  • Caveat: Poor memory efficiency for large datasets.

5. Slicing

  • Feature: Insert or join multiple elements at arbitrary positions.
  • Use case: When flexible operations are needed.
  • Caveat: You must correctly specify indices.

6.2 Recommended methods by use case

  • Small datasets: + operator is simple and suitable.
  • Large datasets: The memory-efficient extend() is recommended.
  • Adding a single element: For loops and similar cases, append() is suitable.
  • Inserting at a specific position: Use insert() or slicing.
  • Adding multiple elements at a specific position: Slicing is flexible and effective.

6.3 Cautions when merging

  • Understand the difference between append() and extend() and use them appropriately.
  • Be mindful of performance with large datasets, and prioritize extend().
  • Operations that create new lists (the + operator) should be used with attention to memory usage.
  • Avoiding side-effect risks: Use slicing when copying lists.
  • Prevent type mismatch errors by ensuring the data being joined is iterable.

6.4 Next steps

Use this article as a guide to practice manipulating lists in Python. Try practicing list operations and choose the joining method that fits your purpose so you can write efficient code. Recommended exercises:
  1. Try each joining method:
  • Compare execution speed with small and large lists.
  1. Intentionally test error cases:
  • Perform operations that include type mismatches and invalid indices to understand the resulting errors.
  1. Consider flexible operations:
  • Implement custom joining operations by combining slicing with extend().
By deepening your understanding of list joining, you’ll be able to write more efficient code with fewer mistakes.

Frequently Asked Questions (FAQ)

Q1: What is the difference between append() and extend()?

A1:
  • append(): Adds a single element to the end of the list. If that element is a list, it will be added as a nested element. Example:
  list1 = [1, 2, 3]
  list1.append([4, 5])
  print(list1)  ## Output: [1, 2, 3, [4, 5]]
  • extend(): Expands another list or iterable (such as tuples or sets) and adds its elements to the end of the list. It does not create nesting. Example:
  list1 = [1, 2, 3]
  list1.extend([4, 5])
  print(list1)  ## Output: [1, 2, 3, 4, 5]

Q2: Which method is best for concatenating large amounts of data?

A2: When concatenating large amounts of data, using extend() is best. Because it modifies the original list in place, it avoids creating a new list like the + operator does, reducing memory usage. Example:
list1 = [i for i in range(1000000)]
list2 = [j for j in range(1000000)]

list1.extend(list2)  ## Memory efficient

Q3: How should you use list concatenation with slicing?

A3: Using slicing lets you insert or join elements at a specific position in a list. It’s useful when you want to insert another list into part of a list, as shown below. Example:
list1 = [1, 5]
list1[1:1] = [2, 3, 4]
print(list1)  ## Output: [1, 2, 3, 4, 5]
Slicing is flexible and suitable when inserting multiple elements at once. However, be careful not to specify the wrong index.

Q4: Which should you choose: the + operator or extend()?

A4:
  • For small lists, the + operator is concise and suitable. Example:
  list1 = [1, 2]
  list2 = [3, 4]
  result = list1 + list2
  print(result)  ## Output: [1, 2, 3, 4]
  • For large lists, choosing extend() is superior in terms of memory efficiency and speed.

Q5: What common errors occur when concatenating lists?

A5: The following errors are commonly seen.
  1. TypeError: ‘int’ object is not iterable
  • Occurs when extend() is given a non-iterable (e.g., an integer). Solution: Pass an iterable (e.g., a list or tuple). Example:
   list1 = [1, 2, 3]
   list1.extend([4])  ## Correct usage
  1. Unexpected creation of nested lists
  • When you add a list with append(), the entire list is treated as a single element. Solution: Use extend() if you want to avoid nesting.

Q6: Which list concatenation method is the fastest?

A6: When performance is a priority, extend() is the most suitable. Especially when concatenating large amounts of data, extend() is more efficient than the + operator. Example: Below is an example of performance comparison results (results depend on the environment).
MethodExecution time (seconds)
+ operator0.08
extend()0.03