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

List of Strings[‘Monday’, ‘Tuesday’, ‘Wednesday’]
List of Integers[12, 45, 56]
List of Floats[5.67, 7.89, 87.24]
Mixed List[‘Milk’, 2, 12.98]
List of Lists[[2, 4, 5], [6, 7, 8], [8, 9, 1]]

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.

[1, 0, 9, 2, 1]
Python One-Liner Trick 9 - Nested List Comprehension

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.

<map object at 0x000001E5DC37BFD0>

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

[1, 0, 9, 2, 1]
Mastering the Python Map Function [+Video]

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)).

[‘1’, ‘0’, ‘9’, ‘2’, ‘1’]

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.

[1, 0, 9, 2, 1]
Python List Methods

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.

[1, 0, 9, 2, 1]

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.

[1, 0, 9, 2, 1]

๐ŸŒŸ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.

[1, 0, 9, 2, 1]
Python Math Module [Ultimate Guide]

๐ŸŒŸ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