 # 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

## Precision Handling in PythonPrecision Handling in Python

Precision Handling in Python Python in its definition allows handling the precision of floating-point numbers in several ways using different functions. Most of them are defined under the “math” module. Some of the most used operations are discussed in this

## Reverse words in a given String in PythonReverse words in a given String in Python

Reverse words in a given String in Python We are given a string and we need to reverse words of a given string? Examples: Input : str = geeks quiz practice code Output : str = code practice quiz geeks

## Bar Plot in MatplotlibBar Plot in Matplotlib

Bar Plot in Matplotlib A bar plot or bar chart is a graph that represents the category of data with rectangular bars with lengths and heights that is proportional to the values which they represent. The bar plots can be

## Python – Get first and last elements of a listPython – Get first and last elements of a list

Python | Get first and last elements of a list Sometimes, there might be a need to get the range between which a number lies in the list, for such applications we require to get the first and last element

## Working with csv files in PythonWorking with csv files in Python

Working with csv files in Python This article explains how to load and parse a CSV file in Python. First of all, what is a CSV ? CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a

Python | Uploading images in Django In most of the websites, we often deal with media data such as images, files etc. In django we can deal with the images with the help of model field which is ImageField. In this

## Python map() functionPython map() function

Python map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.) Syntax : map(fun, iter) Parameters : fun : It is a function

## How to get file extension in Python?How to get file extension in Python?

In Python, we can extract the file extension using either of the two different approaches discussed below – Method 1: Using Python os module splitext() function This function splits the file path string into file name and file extension into