Convert a number to a list of integers in Python

In this article, we will discuss different ways to convert a number to a list of integers in Python.

Table Of Contents

Introduction

Suppose we have a number, and we want to generate a list of digits in that number. For example, if the number is,

1459

Then the generated list of digits should be,

[1, 4, 5, 9]

There are various methods to achieve this, Let’s discuss them one by one.

Advertisements

Method 1: Using List Comprehension

First, convert the given number to a string, then iterate over all the characters in that string. During iteration, convert each character to an integer and store it in a new list. All this can be done in a single line using the list comprehension. Let’s see an example.

num = 1459

# convert an integer to a list of integers
listOfInts = [int(ch) for ch in str(num)]

print(listOfInts)

Output:

[1, 4, 5, 9]

It converted the given integer to a list of integers.

If number has a decimal too. Then we can skip the conversion to int for that dor only. Let’s see another example,

num = 1459.56

# convert a number to a list of integers
listOfInts = [int(ch) if ch!= '.' else ch for ch in str(num)]

print(listOfInts)

Output:

[1, 4, 5, 9, '.', 5, 6]

Method 2: Using the map() function

In Python, the map() function accepts two arguments. First is a function, and second is a sequence. Then it applies the given function on all the elements of the given sequence, and store the results in a mapped project. Now, to convert a given number to a list of integers, we will use this map() function.

First of all, we will convert the given number to a string using the str() function. Then we will pass a function int() and the converted string as an argument to the map() function. After that, the map() function will iterate over all the characters in that string, and apply the int() function on each character of that string. It will store the resultant values to the mapped object. Then we can convert the mapped object to a list. This way we will have a list of integers.

Let’s see the complete example,

num = 1459

# convert a number to a list of integers
listOfInts = list(map(int, str(num)))

print(listOfInts)

Output:

[1, 4, 5, 9]

Method 3 using enumerate() function

Convert the given number to a string, then pass that string to the enumerate() function. It yields pairs containing a index position and character at that index position. Then for each index position, convert the character to an integer, and store that in a new list, using List comprehension. Let’s see an example,

num = 1459

# convert a number to a list of integers
listOfInts = [int(ch) for index, ch in enumerate(str(num))]

print(listOfInts)

Output:

[1, 4, 5, 9]

It converted the given integer to a list of digits.

Method 4: Using for-loop

Create an empty list. Then iterate over all characters in string using a for loop. During iteration, convert each character to integer, and append it to the empty list created in first step. When loop ends, out list will have all digits from the given number. Let’s see an example,

num = 1459

# create an empty list
listOfInts = []

# Convert a number to a list of integers
for ch in str(num):
    listOfInts.append(int(ch))

print(listOfInts)

Output:

[1, 4, 5, 9]

It converted the given integer to a list of digits.

Summary

We learned to convert a number to a list of integers in Python.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top