Convert Number to Digit List in Python

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13623{{"`Convert Number to Digit List in Python`"}} python/variables_data_types -.-> lab-13623{{"`Convert Number to Digit List in Python`"}} python/numeric_types -.-> lab-13623{{"`Convert Number to Digit List in Python`"}} python/strings -.-> lab-13623{{"`Convert Number to Digit List in Python`"}} python/type_conversion -.-> lab-13623{{"`Convert Number to Digit List in Python`"}} python/lists -.-> lab-13623{{"`Convert Number to Digit List in Python`"}} python/tuples -.-> lab-13623{{"`Convert Number to Digit List in Python`"}} python/function_definition -.-> lab-13623{{"`Convert Number to Digit List in Python`"}} python/data_collections -.-> lab-13623{{"`Convert Number to Digit List in Python`"}} python/build_in_functions -.-> lab-13623{{"`Convert Number to Digit List in Python`"}} end

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:

  1. Convert the input number n to a string.
  2. Use the map() function combined with the int function to convert each character in the string to an integer.
  3. 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.

Other Python Tutorials you may like