Sai A Sai A
Updated date Sep 21, 2023
In this blog, we will learn various methods to convert integers into lists in Python. Explore multiple methods and sample code and outputs for each method.

Introduction:

Python offers multiple ways to solve common programming challenges. One such challenge is converting an integer into a list. This operation can be required in various scenarios, such as splitting a number into its individual digits or representing it as a list for further processing. In this blog, we will explore several methods to convert an integer to a list in Python.

Method 1: Using List Comprehension

The first method we will explore involves using list comprehension, a concise and Pythonic way to create lists. We can iterate through the digits of the integer and append them to a list.

def int_to_list_method_1(number):
    return [int(digit) for digit in str(number)]

# Example usage
number = 12345
result = int_to_list_method_1(number)
print(result)

Output:

[1, 2, 3, 4, 5]

In this method, we use str(number) to convert the integer to a string, allowing us to iterate through its individual digits. We then use list comprehension to convert each character (digit) back to an integer and store it in the resulting list. This approach is concise and readable.

Method 2: Using Map and int

Another way to convert an integer to a list of its digits is by using the map function along with the int constructor.

def int_to_list_method_2(number):
    return list(map(int, str(number)))

# Example usage
number = 9876
result = int_to_list_method_2(number)
print(result)

Output:

[9, 8, 7, 6]

In this method, we first convert the integer to a string using str(number), similar to the previous method. Then, we use the map function to apply the int constructor to each character (digit) in the string, effectively converting them to integers. Finally, we use list() to convert the map object into a list.

Method 3: Using While Loop and Modulus

Let's explore an alternative approach that doesn't rely on string conversions. We can use a while loop to repeatedly extract the least significant digit of the integer using the modulus operator (%).

def int_to_list_method_3(number):
    digits = []
    while number > 0:
        digits.insert(0, number % 10)
        number //= 10
    return digits

# Example usage
number = 54321
result = int_to_list_method_3(number)
print(result)

Output:

[5, 4, 3, 2, 1]

In this method, we initialize an empty list digits to store the individual digits of the integer. We use a while loop to repeatedly extract the least significant digit using the modulus operator %. The extracted digit is then inserted at the beginning of the digits list using the insert() method, ensuring that the digits are in the correct order. We also update the number by performing integer division number //= 10 to remove the least significant digit.

Method 4: Using Recursion

For those who prefer a recursive approach, we can write a function that breaks down the integer recursively into its digits.

def int_to_list_method_4(number):
    if number < 10:
        return [number]
    else:
        return int_to_list_method_4(number // 10) + [number % 10]

# Example usage
number = 98765
result = int_to_list_method_4(number)
print(result)

Output:

[9, 8, 7, 6, 5]

In this method, we define a recursive function int_to_list_method_4(). If the number is less than 10 (a single digit), we return a list containing that digit. Otherwise, we recursively call the function with number // 10 to process the remaining digits and then concatenate the result with [number % 10], which gives us the least significant digit.

Method 5: Using itertools and divmod

The divmod function can be used in combination with the itertools module to convert an integer to a list of its digits.

import itertools

def int_to_list_method_5(number):
    return list(itertools.chain.from_iterable(divmod(number, 10)))

# Example usage
number = 123456
result = int_to_list_method_5(number)
print(result)

Output:

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

In this method, we use the divmod() function, which returns a tuple containing the quotient and remainder when number is divided by 10. We then use itertools.chain.from_iterable() to flatten the resulting tuples into a single iterable, which is then converted to a list.

Conclusion:

In this blog, we have explored five different methods to convert an integer into a list of its digits in Python. 

  • List Comprehension: This method is concise and easy to read, making it a good choice for most cases.
  • Using Map and int: It offers a functional programming approach and can be efficient for large numbers.
  • Using While Loop and Modulus: This method avoids string conversions and is useful when working with integers directly.
  • Using Recursion: A recursive approach for those who prefer it, providing a clean solution.
  • Using itertools and divmod: A unique approach that uses Python's itertools and divmod functions for a compact solution.

Comments (0)

There are no comments. Be the first to comment!!!