Using List Comprehension
To split integer into digits in Python:
- Use the
str()
to transform the specified integer to a string. - Use a list comprehension to loop over converted String.
- Use
int()
to convert every substring to an integer in each iteration.
1 2 3 4 5 |
number = 243689 list_of_digits = [int(i) for i in str(number)] print(list_of_digits) |
This code will display the following results.
1 2 3 |
[2, 4, 3, 6, 8, 9] |
While using list comprehension in the above code block, we converted the given number
to a string using the str()
method. Then, we used list comprehension to iterate over the converted string..
We used int()
in each iteration to convert the sub-string to an integer. All this code (int(i) for i in str(number)
) is enclosed in []
to get list-type results.
Using for
Loop
To split number into digits in Python:
- Use the
str()
to transform the specified integer to a string. - Use the
for
loop to iterate over the converted string. - Use
int()
to convert every substring to an integer in each iteration. Then, pass the converted integer to theappend()
method to append elements to a list.
1 2 3 4 5 6 7 8 |
number = 243689 string = str(number) list_of_digits = [] for i in string: list_of_digits.append(int(i)) print(list_of_digits) |
First, we used str()
and passed the specified number
to this function to convert from integer to string. Then, we used a for
loop to iterate over the converted string.
In each iteration, we first converted the sub-string to an integer and passed it to the append() method, which is used to append items to a list named list_of_digits
.
Further reading:
Using map()
with list()
& str.split()
To split the given number into digits in Python:
- Use
str()
to convert the specifiednumber
to string to make it iterable. - Use
map()
to run a particular function for each substring in the iterable (we created in the previous step). - Use
list()
to create a list object.
1 2 3 4 5 |
number = 243689 list_of_digits = list(map(int, str(number))) print(list_of_digits) |
After executing the above code successfully, we will get the following output on the console.
1 2 3 |
[2, 4, 3, 6, 8, 9] |
For this code example, we used the str()
method, which took the number
as a parameter and converted it to a string. Why did we need to convert it? Because we want to make it iterable to use the map()
function.
The map()
took two parameters, the first parameter is the int
function that we wanted to execute, and the second is the iterable returned by str(number)
. Here, the map()
function converted each element (which is sub-string here) of an iterable to an integer.
After that, we used the list()
function to create a list object. Here, the list object is a collection that is ordered and changeable.
Suppose we are given a number in string format already; how can we convert them to digits? In that case, we can do as follows:
- Use
str.split()
to split the given string. - Use the
map()
function to iterate over each list element and convert it to an integer. - Use the
list()
function to convert it to a list object.
1 2 3 4 5 6 |
string = "2 4 3 6 8 9" my_list = string.split() list_object = list(map(int, my_list)) print(list_object) |
Now, we will get the output as follows.
1 2 3 |
[2, 4, 3, 6, 8, 9] |
The map()
are list()
discussed while explaining the code using map()
with list()
. In this code snippet, we used a new function, str.split()
, it is used to split the specified string based on the provided separator.
The separator can be whitespace, a comma, a dash or anything. Here, we are splitting our string using its default separator, which is a whitespace. You can find more on that here.
That’s all about how to split Integer into Digits in Python.