# Python – Convert number to list of integers

## Python | Convert number to list of integers

The interconversion of data types is a problem that is quite common in programming. Sometimes we need to convert a single number to list of integers and we don’t wish to spend several lines of codes doing it. Hence having ways to perform this task using shorthands comes handy. Let’s discuss ways in which this can be performed.

Method #1 : Using list comprehension
List comprehension can be used as a shorthand to the longer format of naive method. In this method, we convert the number to string and then extract its each character and re convert it to integer.

 `# Python3 code to demonstrate ` `# conversion of number to list of integers` `# using list comprehension` ` ` `# initializing number ` `num ``=` `2019` ` ` `# printing number ` `print` `(``"The original number is "` `+` `str``(num))` ` ` `# using list comprehension` `# to convert number to list of integers` `res ``=` `[``int``(x) ``for` `x ``in` `str``(num)]` ` ` `# printing result ` `print` `(``"The list from number is "` `+` `str``(res))`

Output:

```The original number is 2019
The list from number is [2, 0, 1, 9]```

Method #2 : Using `map()`
`map `function can be used to perform the following task converting each of the string converted number to the desired integer value to be reconverted to the list format.

 `# Python3 code to demonstrate ` `# conversion of number to list of integers` `# using map()` ` ` `# initializing number ` `num ``=` `2019` ` ` `# printing number ` `print` `(``"The original number is "` `+` `str``(num))` ` ` `# using map()` `# to convert number to list of integers` `res ``=` `list``(``map``(``int``, ``str``(num)))` ` ` `# printing result ` `print` `(``"The list from number is "` `+` `str``(res))`

Output:

```The original number is 2019
The list from number is [2, 0, 1, 9]```

Last Updated on October 28, 2021 by admin

## Logging in PythonLogging in Python

Logging in Python Logging is a means of tracking events that happen when some software

## Python – Extract words from given stringPython – Extract words from given string

Python | Extract words from given string We sometimes come through situations where we require

## Select row with maximum and minimum value in Pandas dataframeSelect row with maximum and minimum value in Pandas dataframe

Let’s see how can we select row with maximum and minimum value in Pandas dataframe

## Reverse string in Python (All different ways)Reverse string in Python (All different ways)

Reverse string in Python (5 different ways) Python string library does’nt support the in-built “reverse()”

## StringIO Module in PythonStringIO Module in Python

StringIO Module in Python The StringIO module is an in-memory file-like object. This object can be used

## Python Main FunctionPython Main Function

Python Main Function Main function is like the entry point of a program. However, Python

## Break a list into chunks of size N in PythonBreak a list into chunks of size N in Python

Python – Break a list into chunks of size N   Method 1: Using yield

## Python – Add two list elementsPython – Add two list elements

Python | Adding two list elements There can be many situations in which one requires