
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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]
Advertisements