5 Best Ways to Concatenate a List of Integers in Python

πŸ’‘ Problem Formulation:

In this article, we explore how to concatenate a list of integers in Python – a common task in data processing and manipulation. Given a list of integers, for example, [1, 2, 3], we want to join these elements into a single integer, 123. The methods below provide multiple ways to achieve this, catering to different scenarios and requirements.

Method 1: Using str.join() and List Comprehension

This method involves converting each integer to a string and then concatenating them using the join method. It is concise and efficient for combining elements of a list into a single string.

Here’s an example:

numbers = [1, 2, 3]
concatenated = int("".join(str(i) for i in numbers))

Output: 123

The list comprehension [str(i) for i in numbers] creates a list of strings, which is then concatenated into a single string by "".join(). The resulting string is converted back to an integer with int().

Method 2: Using reduce() Function

The reduce() function can be used to apply a rolling computation to pair of elements in a list. For concatenating integers, we define a lambda function that merges two integers accordingly.

Here’s an example:

from functools import reduce

numbers = [1, 2, 3]
concatenated = reduce(lambda x, y: x * 10**len(str(y)) + y, numbers)

Output: 123

The lambda function inside the reduce() multiplies the first number by a power of 10 that corresponds to the number of digits in the second number and adds the second number, effectively concatenating them as integers.

Method 3: Using str() and + Operator

Another approach is to manually concatenate each integer after converting it to a string using the + operator, which is straightforward and simple.

Here’s an example:

numbers = [1, 2, 3]
concatenated = int("".join([str(i) for i in numbers]))

Output: 123

This snippet creates a new string by initializing an empty string and then appending each integer cast as a string. In the end, the concatenated string is converted back to an integer.

Method 4: By Constructing the Number Manually

Building the concatenated number manually involves iterating over the list and adding each integer to the running total after adjusting its magnitude based on position.

Here’s an example:

numbers = [1, 2, 3]
concatenated = 0

for n in numbers:
    concatenated = concatenated * 10 + n

Output: 123

This code initializes a variable concatenated to zero and for each integer in the list, multiplies concatenated by 10 (to shift digits to the left) and then adds the current integer.

Bonus One-Liner Method 5: Using functools.reduce() with Operator

An even shorter version of the reduce method uses the multiplication and addition operators imported from the operator module. It is compact and leverages built-in functions for brevity.

Here’s an example:

from functools import reduce
from operator import add, mul

numbers = [1, 2, 3]
concatenated = reduce(lambda acc, x: mul(acc, 10) + x, numbers)

Output: 123

This one-liner variant of the reduce method applies a lambda function that multiplies the accumulated value by 10 and adds the current value, effectively building the concatenated number.

Summary/Discussion

  • Method 1: Using str.join() and List Comprehension. Strengths: Concise and Pythonic. Weakness: Requires conversion to and from strings.
  • Method 2: Using reduce() Function. Strengths: Functional programming approach, elegant. Weakness: Can be less readable for those unfamiliar with reduce.
  • Method 3: Using str() and + Operator. Strengths: Straightforward and easy to understand. Weakness: Not as compact as Method 1.
  • Method 4: By Constructing the Number Manually. Strengths: Efficient with no need for string conversions. Weakness: More verbose.
  • Method 5: One-Liner with functools.reduce() and Operator. Strengths: Compact, functional one-liner. Weakness: Less explicit, harder to debug.