Problem Formulation and Solution Overview
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.
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] |
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] |
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] |
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] |
๐Another Finxter Favorite!
Twice as Fast as other Methods!
No String Conversion Required!
Summary
Good Luck & Happy Coding!
Programmer Humor – Blockchain


At university, I found my love of writing and coding. Both of which I was able to use in my career.
During the past 15 years, I have held a number of positions such as:
In-house Corporate Technical Writer for various software programs such as Navision and Microsoft CRM
Corporate Trainer (staff of 30+)
Programming Instructor
Implementation Specialist for Navision and Microsoft CRM
Senior PHP Coder