5

Convert a number to a list of integers in Python

 1 year ago
source link: https://thispointer.com/convert-a-number-to-a-list-of-integers-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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,

Then the generated list of digits should be,

[1, 4, 5, 9]
[1, 4, 5, 9]

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

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)
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]
[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)
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]
[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)
num = 1459

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

print(listOfInts)

Output:

[1, 4, 5, 9]
[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)
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]
[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)
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]
[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.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK