How to convert a number to a list in Python

There are various methods of taking multiple inputs in a single line.

Method 1:
using map() function

syntax:
variable = list(map(int, str(number)))

Don’t know how to take multiple inputs in one line in Python? Read: How to take multiple inputs in one line / single line in Python

Example 1:

# Python program to understand how to convert a number to a list in Python
# www.codewindow.in
# using map() function
number = 4569
x = list(map(int, str(number)))
print(x)

Output:

[4, 5, 6, 9]

Explanation:
Here the numeric input has been converted to list. Each single digit numbers are an integer inside the list.

Method 2:
Using List comprehension

Example 1:

# Python program to understand how to convert a number to a list in Python
# www.codewindow.in
# using list comprehension
number = 4569
x = [int(i) for i in str(number)]
print(x)

Output:

[4, 5, 6, 9]

Explanation:
Here the loop returns all single digit variables without any separator to a single list as three different integers.


You Missed

Also Checkout