5 Best Ways to Concatenate Two Arrays in Python

πŸ’‘ Problem Formulation: Imagine you have two arrays, or lists, of data in Python: array1 = [1, 2, 3] and array2 = [4, 5, 6]. Your task is to combine these arrays into a single sequence: array3 = [1, 2, 3, 4, 5, 6]. This operation, known as concatenation, needs to be efficient and straightforward. In this article, we’ll explore five methods to achieve this in Python, each with its own use case and performance characteristics.

Method 1: Using the Plus Operator

To concatenate two arrays in Python, the most intuitive method is using the plus + operator. It’s as simple as adding two numbers; only here, you are ‘adding’ two lists. The operation is both readable and straightforward, providing a clear indication of its purpose in the code. Keep in mind this method creates a new list.

Here’s an example:

array1 = [1, 2, 3]
array2 = [4, 5, 6]
array3 = array1 + array2
print(array3)

Output:

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

The code above takes two arrays and joins them together with the + operator, resulting in a new array that is a combination of both.

Method 2: Using the extend() Method

The extend() method alters the first array by appending the second array’s elements, effectively concatenating them without creating a new list. This is efficient when the original array is no longer needed in its initial form.

Here’s an example:

array1 = [1, 2, 3]
array2 = [4, 5, 6]
array1.extend(array2)
print(array1)

Output:

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

This snippet demonstrates the in-place concatenation of array2 to array1 using the very efficient extend() method, which avoids creating a new list.

Method 3: Using the itertools.chain() Function

The itertools.chain() function from Python’s itertools library is a powerful tool for chaining together multiple iterables, including arrays. This method is memory efficient and suitable for large or infinite series because it returns an iterator instead of creating a new list.

Here’s an example:

import itertools

array1 = [1, 2, 3]
array2 = [4, 5, 6]
array3 = list(itertools.chain(array1, array2))
print(array3)

Output:

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

In this code example, itertools.chain() is used to create an iterator that goes through each argument iteratively, and list() is then called to create a list from this iterator.

Method 4: Using List Comprehension

List comprehension offers a compact way of concatenating arrays. It is not only Pythonic but also highly readable. This approach is useful when additional conditions or processing is required while concatenating.

Here’s an example:

array1 = [1, 2, 3]
array2 = [4, 5, 6]
array3 = [item for array in [array1, array2] for item in array]
print(array3)

Output:

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

Here, the list comprehension iterates over a list of the original two arrays and, for each array, iterates over its items to create a new concatenated list.

Bonus One-Liner Method 5: Using the Asterisk Operator

The asterisk operator * can be used as a one-liner for unpacking the elements of the arrays into a new list. This method is quite succinct and Pythonic while maintaining the clarity of the operation.

Here’s an example:

array1 = [1, 2, 3]
array2 = [4, 5, 6]
array3 = [*array1, *array2]
print(array3)

Output:

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

This snippet demonstrates the use of the unpacking operator * to concatenate elements of both arrays into a new list.

Summary/Discussion

  • Method 1: Plus Operator. Simple and clear. Creates a new list. Not memory efficient for large lists.
  • Method 2: extend() Method. Efficient and in-place. Alters the original list, which might not be desirable.
  • Method 3: itertools.chain() Memory-efficient and suitable for large or infinite series. Requires additional steps to turn the result into a list.
  • Method 4: List Comprehension. Pythonic and flexible for further processing. Slightly less readable for newcomers.
  • Bonus Method 5: Asterisk Operator. Concise and clean. Creates a new list and may not be well-known to some Python programmers.