# How to Convert an Integer to a List in Python

4.3/5 - (3 votes)

## Problem Formulation and Solution Overview

This article will show you how to convert a single Integer into a List in Python.

Python has several built-in Data Type Structures: one of them being a List. The List Data Structure is made up of square brackets (`[]`) containing elements separated by a comma (`,`). These elements can be made up of Strings, Integers, Floats, Mixed Types, or other Data Structures, such as a List of Lists.

Sample Lists

For fun, let’s convert the circumference of the Moon (10,921 km) into a List of Integers.

💡Note: Before creating a List of Lists from an Integer, it must be converted into a String as Strings are iterable: Integers are not.

💬 Question: How would we write code to convert an Integer into a List?

We can accomplish this task by one of the following options:

## Method 1: Use List Comprehension

This example uses List Comprehension to convert an Integer to a String, extract each element and convert it to a List of Integers.

```cir_moon = 10921
cir_list = [int(x) for x in str(cir_moon)]
print(cir_list)```

Above declares the circumference of the Moon (in km) and saves it to `cir_moon` as an Integer.

Next, List Comprehension is used. This converts `cir_moon` to a String (`str(cir_moon)`), allowing for iteration. Each element is then extracted, converted to an Integer (`int(x))` and appended to `cir_list`.

The results are output to the terminal as a List of Integers.

## Method 2: Use list and map()

This example uses List and the `map()` function to convert an Integer to a String, extract each element and convert it to a List of Integers.

```cir_moon = 10921
cir_list = list(map(int, str(cir_moon)))
print(cir_list)```

Above declares the circumference of the Moon (in km) and saves it to `cir_moon`.

Next, the `map()` function is called and converts `cir_moon` to a String (`str(moon)`). Then, each element is converted to an Integer (`int`).

If output to the terminal at this point, a `map()` object similar to below would display.

However, these results are converted into a List, saved to `cir_list`, and output to the terminal as a List of Integers.

## Method 3: Use List Comprehension and List

This example uses List Comprehension and List to convert an Integer to a String, extract each element and convert it to a List of Integers.

```cir_moon = str(10921)
cir_list = [int(x) for x in list(cir_moon)]
print(cir_list)```

Above declares the circumference of the Moon (in km), converts this value (`10921`) into a String representation and saves it to `cir_moon` to make it iterable.

Next, List Comprehension is used. Then, `cir_moon` is converted to a List of Strings (`list(cir_moon)`).

Each String element is then converted to an Integer (`int(x)`) and appended to `cir_list`.

The results are output to the terminal as a List of Integers.

## Method 4: Use a For Loop

This example uses a For loop to iterate through a String and convert it to a List of Integers.

```cir_moon = str(10921)
cir_list = []

for c in cir_moon:
cir_list.append(int(c))
print(cir_list)```

Above declares the circumference of the Moon (in km), converts this value (`10921`) into a String and saves it to `cir_moon` to make it iterable. Then, an empty List, `cir_list`, is created.

Next, a For loop is instantiated to iterate through the elements of `cir_moon`. Each element is accessed, converted to an Integer (`int(c)`) and appended to `cir_list`.

The results are output to the terminal as a List of Integers.

## Method 5: Use * and map()

This example uses the `*` operator and `map()` to iterate through a String and convert it to a List of Integers.

```cir_list = [*map(int, str(10921))]
print(cir_list)
```

A compact and efficient resolution to the above problem!

The infamous * (or unpacking) operator unpacks all values from any iterable object. In this case, the String, `10921`. Each element is extracted, converted to an Integer (`int`), appended and saved to `cir_list`.

The results are output to the terminal as a List of Integers.

🌟A Finxter Favorite!
Simple yet Efficient!

## Method 6: Use the Math Library

The upside of using the `math` library along with the `math.ceil()` and `math.log()` functions is that you do not have to convert the Integer to a String prior to execution.

```cir_moon = 10921
x = [(cir_moon//(10**i))%10 for i in range(math.ceil(math.log(cir_moon, 10))-1, -1, -1)]
print(x)```

Above declares the circumference of the Moon (in km) and saves it to `cir_moon`.

Next, List Comprehension is used in conjunction with `math.ceil()` and `math.log()` to extract an Integer into separate digits. Done in a snap without converting to a String!

The results are output to the terminal as a List of Integers.

🌟Another Finxter Favorite!
Twice as Fast as other Methods!
No String Conversion Required!

## Summary

These six (6) methods of converting an Integer to a List should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!

## Programmer Humor – Blockchain “Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source – xkcd