
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Convert number to list of integers in Python
As part of data manipulation in Python we may sometimes need to convert a given number into a list which contains the digits from that number. In this article we'll see the approaches to achieve this.
With list comprehension
In the below approach we apply the str function to the given number and then convert into integer through identity function. Finally we wrap the result into a list.
Example
numA = 1342 # Given number print("Given number : \n", numA) res = [int(x) for x in str(numA)] # Result print("List of number: \n",res)
Output
Running the above code gives us the following result −
Given number : 1342 List of number: [1, 3, 4, 2]
With map and str
We fast apply the str function to the given number. Then apply the in function repeatedly using map. Finally keep the result inside a list function.
Example
numA = 1342 # Given number print("Given number : \n", numA) res = list(map(int, str(numA))) # Result print("List of number: \n",res)
Output
Running the above code gives us the following result −
Given number : 1342 List of number: [1, 3, 4, 2]
- Related Articles
- Convert list of numerical string to list of Integers in Python
- Convert a list of multiple integers into a single integer in Python
- Convert number string to integers in C++
- Program to find the number of unique integers in a sorted list in Python
- Convert list of string to list of list in Python
- Convert list of tuples to list of list in Python
- Python - Convert list of string to list of list
- Program to find number of operations needed to convert list into non-increasing list in Python
- Convert list of strings to list of tuples in Python
- Convert list of tuples to list of strings in Python
- What is the lambda expression to convert array/List of String to array/List of Integers in java?
- Python - Convert List of lists to List of Sets
- Converting all strings in list to integers in Python
- Convert string enclosed list to list in Python
- Convert list of tuples into list in Python

Advertisements