Introduction
In Python, it is often necessary to convert a number into a list of its digits. This can be useful for various applications, such as performing mathematical operations on individual digits or manipulating numbers in a more granular way. In this challenge, you will be tasked with writing a function that takes a number as input and returns a list of its digits.
Digitize Number
Write a function digitize(n) that takes a non-negative integer n as input and returns a list of its digits. The function should accomplish this by performing the following steps:
- Convert the input number
nto a string. - Use the
map()function combined with theintfunction to convert each character in the string to an integer. - Return the resulting list of integers.
For example, if the input number is 123, the function should return the list [1, 2, 3].
def digitize(n):
return list(map(int, str(n)))
digitize(123) ## [1, 2, 3]
Summary
In this challenge, you have learned how to convert a number into a list of its digits using Python. By using the map() function and the int function, you can easily convert each character in a string to an integer and return a list of those integers. This technique can be useful for various applications, such as performing mathematical operations on individual digits or manipulating numbers in a more granular way.