5 Best Ways to Convert a Number to a List of Integers in Python

πŸ’‘ Problem Formulation: In Python programming, it’s common to have a scenario where you need to take a single multi-digit number and break it down into its constituent digits as a list of integers. For instance, converting the integer 12345 to the list [1, 2, 3, 4, 5].

Method 1: Using ‘map’ and ‘int’

One robust method for converting a number into a list of its digits is to treat the number as a string, iterate over each character, and convert them back into integers. The built-in map() function makes this concise. Here, map() applies the int function to each character in the string representation of the number.

Here’s an example:

number = 12345
digits = list(map(int, str(number)))
print(digits)

Output: [1, 2, 3, 4, 5]

This code snippet first converts the integer to a string, and then map() applies the int() function to each character in the string, which gives us a map object containing the digits. We convert this map object to a list to get our final list of integers.

Method 2: List Comprehension

Python’s list comprehensions provide a succinct way to create lists. In this case, a list comprehension can iterate over each character in the string version of the number and cast it to an integer, resulting in a list of digits.

Here’s an example:

number = 12345
digits = [int(digit) for digit in str(number)]
print(digits)

Output: [1, 2, 3, 4, 5]

The list comprehension iterates through each character in the number’s string form, converting each character back to an integer. This is both elegant and efficient.

Method 3: Using ‘for’ loop explicitly

For those who prefer a more traditional approach, a for loop can be used to iterate through each character in the number’s string representation, cast it to an integer, and append it to a list.

Here’s an example:

number = 12345
digits = []
for digit in str(number):
    digits.append(int(digit))
print(digits)

Output: [1, 2, 3, 4, 5]

This block of code manually iterates through the string representation of the number, converts each character to an integer using int(), and appends it to the list digits. While not as concise as a list comprehension, it’s often easier for beginners to read and understand.

Method 4: Using ‘divmod’

The divmod() function can be used to successively divide the number by 10, extracting each digit. This method works well with integers and requires a while loop to process the digits.

Here’s an example:

number = 12345
digits = []
while number:
    number, digit = divmod(number, 10)
    digits.append(digit)
digits.reverse()
print(digits)

Output: [1, 2, 3, 4, 5]

This approach divides the number by 10, getting the remainder as the rightmost digit and the quotient as the new number to be processed in the next iteration. After extracting all digits, the list is reversed to match the original order of digits.

Bonus One-Liner Method 5: Using ‘map’ with a generator expression

If you’re looking for a compact solution, a one-liner using map() with a generator expression neatly packs the conversion in a single line of code.

Here’s an example:

number = 12345
digits = list(map(int, (char for char in str(number))))
print(digits)

Output: [1, 2, 3, 4, 5]

This one-liner turns the number into a string and then iterates over each character in a generator expression, which is processed by map() to convert each character to an integer, and then a list is created from the result.

Summary/Discussion

  • Method 1: Using ‘map’ and ‘int’. Strengths: Concise and functional. Weaknesses: May be less readable for beginners.
  • Method 2: List Comprehension. Strengths: Pythonic and readable. Weaknesses: Not as explicit as a loop.
  • Method 3: Using ‘for’ loop explicitly. Strengths: Easy to read and understand. Weaknesses: More verbose than other methods.
  • Method 4: Using ‘divmod’. Strengths: No need to convert to string, works directly with integers. Weaknesses: Not as intuitive, requires reversing the list.
  • Method 5: Using ‘map’ with a generator expression. Strengths: Compact one-liner. Weaknesses: Can be tough to decipher at a glance.