How to make a Python number from the list

Transform a list of numbers into a number in Python with a simple example. Learn how to join a list of digits into a single number with a few lines of code.

Creating a Python Number from a List

Python has an easy way to create a number from a list. This can be done by converting the list into a string, and then using the int() function to convert the string into an integer. To illustrate this, let's consider a list of integers [1, 2, 3, 4, 5].


# Convert the list to a string
list_string = ''.join(str(i) for i in [1, 2, 3, 4, 5])

# Convert the string to an integer using the int() function
list_int = int(list_string)

print(list_int)

The output of the code is 12345, which is the number created from the list. This same technique can be used to create a number from any list of integers.

Answers (0)