# Convert Number to List & Vice Versa in Python (Examples)

Hi! This tutorial will show you how to turn a number into a list and vice-versa in the Python programming language.

First, here is an overview of this tutorial:

Let’s get into the Python code!

## Create Sample Number

Here, we will create the sample number that we will convert into a list in this tutorial. Therefore, in your preferred Python IDE, run the code below to create the sample number. For instance, it is 12345 for our examples.

`number = 12345`

Let’s now see the alternatives to convert this number to a list and vice versa!

## Example 1: Number to List | Turn Number into List Using List Comprehension

In this first example, we will turn the number into a list, using the list comprehension method.

```number_list = [int(i) for i in str(number)]
print(number_list)

# [1, 2, 3, 4, 5]```

The list comprehension above first converts the number from an integer to a string; then it iterates through the string and places each item inside a list while also converting them back to integer so that we have a list of integers: 1, 2, 3, 4 and 5.

You can ascertain that the object is now a list by running the following.

```print(type(number_list))

# <class 'list'>```

Great, it is a list as wanted!

## Example 2: Number to List | Turn Number into List Using For Loop

In this second example, we will use a for loop to transform the number into a list.

```number_list = []
for i in str(number):
number_list.append(int(i))

print(number_list)

# [1, 2, 3, 4, 5]

print(type(number_list))

# <class 'list'>```

Much like the list comprehension in the first example, the for loop iterates through the number as a string, and appends it to the `number_list`, while converting it to an integer, so that the output is an integer list.

## Example 3: Number to List | Turn Number into List Using map() Function

In this third and final example, we will turn the number into a list, using the built-in map() function.

```number_list = list(map(int, str(number)))
print(number_list)

# [1, 2, 3, 4, 5]

print(type(number_list))

# <class 'list'>```

The `map()` function also iterates through the number as a string and applies the integer function to it.

Next, we will demonstrate, with 2 examples, how to convert the list back into a number.

## Example 1: List to Number | Turn List into Number Using a For Loop

In this first example, we transform the list back to a number using a for loop.

```number = 0
for i in number_list:
number = number * 10 + i
print(number)

# 12345

print(type(number))

# <class 'int'>```

The code inside the loop updates the value of “number” in each iteration by first multiplying its current value by 10 and then adding the value of “i”. This effectively concatenates the digits in the list and converts them into a single number.

## Example 2: List to Number | Turn List into Number int() & join() Functions

In this second example, we will use the int() function and the join() function to convert the list to a number.

```number = int(''.join(map(str, number_list)))
print(number)

# 12345

print(type(number))

# <class 'int'>```

In the code above, we used the `map()` function to iteratively apply the `str()` function to the list, to convert the list into a string. Thereafter, we used the `join()` function to concatenate the string and then converted the string into an integer with the `int()` function.

With that, we have demonstrated in this tutorial how to convert a number into a list and vice-versa in the Python programming language. I hope you found it helpful!

## Video, Further Resources & Summary

Do you need more explanations on how to convert a number into a list and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a number into a list and vice-versa in Python.

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a number into a list and vice-versa in Python. In case you have further questions, you may leave a comment below.

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

Subscribe to the Statistics Globe Newsletter