# Over-explaining a bit:
def magic(numList):         # [1,2,3]
    s = map(str, numList)   # ['1','2','3']
    s = ''.join(s)          # '123'
    s = int(s)              # 123
    return s


# How I'd probably write it:
def magic(numList):
    s = ''.join(map(str, numList))
    return int(s)


# As a one-liner  
num = int(''.join(map(str,numList)))


# Functionally:
s = reduce(lambda x,y: x+str(y), numList, '')
num = int(s)


# Using some oft-forgotten built-ins:
s = filter(str.isdigit, repr(numList))
num = int(s)
Answer from Kenan Banks on Stack Overflow
Top answer
1 of 16
187
# Over-explaining a bit:
def magic(numList):         # [1,2,3]
    s = map(str, numList)   # ['1','2','3']
    s = ''.join(s)          # '123'
    s = int(s)              # 123
    return s


# How I'd probably write it:
def magic(numList):
    s = ''.join(map(str, numList))
    return int(s)


# As a one-liner  
num = int(''.join(map(str,numList)))


# Functionally:
s = reduce(lambda x,y: x+str(y), numList, '')
num = int(s)


# Using some oft-forgotten built-ins:
s = filter(str.isdigit, repr(numList))
num = int(s)
2 of 16
59

Two solutions:

>>> nums = [1, 2, 3]
>>> magic = lambda nums: int(''.join(str(i) for i in nums)) # Generator exp.
>>> magic(nums)
123
>>> magic = lambda nums: sum(digit * 10 ** (len(nums) - 1 - i) # Summation
...     for i, digit in enumerate(nums))
>>> magic(nums)
123

The map-oriented solution actually comes out ahead on my box -- you definitely should not use sum for things that might be large numbers:

import collections
import random
import timeit

import matplotlib.pyplot as pyplot

MICROSECONDS_PER_SECOND = 1E6
FUNS = []
def test_fun(fun):
    FUNS.append(fun)
    return fun

@test_fun
def with_map(nums):
    return int(''.join(map(str, nums)))

@test_fun
def with_interpolation(nums):
    return int(''.join('%d' % num for num in nums))

@test_fun
def with_genexp(nums):
    return int(''.join(str(num) for num in nums))

@test_fun
def with_sum(nums):
    return sum(digit * 10 ** (len(nums) - 1 - i)
        for i, digit in enumerate(nums))

@test_fun
def with_reduce(nums):
    return int(reduce(lambda x, y: x + str(y), nums, ''))

@test_fun
def with_builtins(nums):
    return int(filter(str.isdigit, repr(nums)))

@test_fun
def with_accumulator(nums):
    tot = 0
    for num in nums:
        tot *= 10
        tot += num
    return tot

def time_test(digit_count, test_count=10000):
    """
    :return: Map from func name to (normalized) microseconds per pass.
    """
    print 'Digit count:', digit_count
    nums = [random.randrange(1, 10) for i in xrange(digit_count)]
    stmt = 'to_int(%r)' % nums
    result_by_method = {}
    for fun in FUNS:
        setup = 'from %s import %s as to_int' % (__name__, fun.func_name)
        t = timeit.Timer(stmt, setup)
        per_pass = t.timeit(number=test_count) / test_count
        per_pass *= MICROSECONDS_PER_SECOND
        print '%20s: %.2f usec/pass' % (fun.func_name, per_pass)
        result_by_method[fun.func_name] = per_pass
    return result_by_method

if __name__ == '__main__':
    pass_times_by_method = collections.defaultdict(list)
    assert_results = [fun([1, 2, 3]) for fun in FUNS]
    assert all(result == 123 for result in assert_results)
    digit_counts = range(1, 100, 2)
    for digit_count in digit_counts:
        for method, result in time_test(digit_count).iteritems():
            pass_times_by_method[method].append(result)
    for method, pass_times in pass_times_by_method.iteritems():
        pyplot.plot(digit_counts, pass_times, label=method)
    pyplot.legend(loc='upper left')
    pyplot.xlabel('Number of Digits')
    pyplot.ylabel('Microseconds')
    pyplot.show()
🌐
Reddit
reddit.com › r/learnpython › how to turn a list of numbers into an integer?
r/learnpython on Reddit: How to turn a list of numbers into an integer?
June 15, 2021 -

Hi, I'm trying to solve this leetcode style problem. Here is the problem. Given a number, return the intermediate values before it reaches Kaprekar's constant, 6174.

Here are the steps:

Arrange the digits in increasing and decreasing order, giving you two numbers

- Subtract the smaller number from the larger number

- Repeat the above steps until the result is 6174

I keep getting an error on my list comprehensions when I'm trying to turn a list into an int. Any help of advice you can give me would be greatly helpful!

def solve(num):
    while num != 0:
        digit_string = str(num)
        digit_map = map(int, digit_string)
        list1 = list(digit_map)
        list1.sort(reverse=True)
        list2 = list1.copy()
        list2.sort()
        
        num1 = int(str(list1))
        num2 = int(str(list2))
        result = list1 - list2
        
        if result == 6174:
            return num1, num2
        else:
            return solve(num1)
            
num = 1234
print(solve(num))
🌐
DEV Community
dev.to › jabermudez11 › splitting-a-number-into-individual-digits-python-3g8a
Splitting a Number into Individual Digits Python - DEV Community
December 6, 2020 - If you are looking only for the code then here it is number = 12345 numList = [int(digit) for digi... Tagged with python, beginners.
🌐
YouTube
youtube.com › watch
Convert a List of Digits into a Number in Python #coding #python #programming - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published: May 16, 2025
🌐
Java2Blog
java2blog.com › home › python › python list › convert list to integer in python
Convert List to Integer in Python [4 Ways] - Java2Blog
February 28, 2023 - Use the reduce() method with the lambda function to convert the entire list to one integer. ... This code example is the first example’s more concise and professional form. Here, we did the same, multiplied the number by 10, added the current ...
🌐
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › convert number to list & vice versa in python (examples)
Convert Number to Python List & Vice Versa (Example) | Split Digits
March 28, 2023 - 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.
🌐
Reddit
reddit.com › r/learnpython › turn array or list of numbers into a number.
r/learnpython on Reddit: Turn array or list of numbers into a number.
December 5, 2020 -

What i want to say is that how can we turn a list/ array like [1,2,3] into a number like 123 also i would love to know how can we turn it back to list.

If you can share the code and explain it would be appreciated.

🌐
Spark By {Examples}
sparkbyexamples.com › home › python › convert list of integer to an integer in python
Convert List of Integer to an Integer in Python - Spark By {Examples}
3 weeks ago - How to convert a list of integers to an integer by multiplying all values in a Python list? You can convert a list of integers into a single integer using
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python tutorial: how-to guides for everyday python problems" › "how to split an integer into digits in python"
How to Split an Integer Into Digits in Python | Delft Stack
February 14, 2024 - For a negative integer, treat the sign separately and split the absolute value; represent zero as [0]. Choose integer arithmetic when converting through a string is unsuitable, or use map() and str.split() when the input is already a whitespace-separated string of digit tokens. A list comprehension is the most direct choice when the input is an integer and the desired result is a list of digits in their usual left-to-right order.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python-convert-a-list-of-multiple-integers-into-a-single-integer
Python Program to Convert a list of multiple integers into a single integer - GeeksforGeeks
December 17, 2024 - For example, if we have a list like [1, 2, 3] then the output should be ['1', '2', '3']. In Python, there are multiple ways to do this efficiently, some of them are: using functions like map(), re
🌐
Note.nkmk.me
note.nkmk.me › home › python
Convert a List of Strings and a List of Numbers to Each Other in Python | note.nkmk.me
May 19, 2023 - Built-in Functions - str() — Python 3.11.3 documentation · l_n = [-0.5, 0, 1.0, 100, 1.2e-2, 0xff, 0b11] l_n_str = [str(n) for n in l_n] print(l_n_str) # ['-0.5', '0', '1.0', '100', '0.012', '255', '3'] ... Note that numbers may be automatically ...
🌐
Reddit
reddit.com › r/learnpython › convert possible digits to int - list comprehension
r/learnpython on Reddit: Convert possible digits to int - list comprehension
February 25, 2021 -

suppose i have a list full of strings:

list = ["m", "s", "o", "23", "1", "k", "2"]

and i want to take this list and convert all digits into int and keep the string, i tried using:

new_list = [int(val) for val in list if val.isdigit()]

but it throws away all non-digit strings, is there a list comprehension solution to this problem? I know how to solve it using regular for loops but i am trying to avoid that, thanks!

🌐
Medium
medium.com › @nawazmohtashim › converting-a-list-to-integer-in-python-exploring-methods-and-techniques-db527fcc583a
Converting a List to Integer in Python: Exploring Methods and Techniques | by Mohd Mohtashim Nawaz | Medium
December 13, 2023 - Converting a list to an integer in Python can be achieved using various methods, each with its advantages depending on the specific requirements of your task.
🌐
YouTube
youtube.com › codesavant
Split an Integer Into a List of Digits in Python - YouTube
This tutorial shows you how to use the modulo operator and floor division to split an integer into a list of digits in Python 3. In this video, you learn to ...
Published: December 11, 2021
Views: 11K
🌐
Snakify
snakify.org › lists
Lists - Learn Python 3 - Snakify
Sequences in Python are strings, lists, values of the function range() (these are not lists), and some other objects. Here's an example showing the use of the for loop when you are needed to extract all the digits from a string and to make numeric list of them.
🌐
YouTube
youtube.com › watch
How To Convert List To Integer In Python - YouTube
How To Convert List To Integer In Python? This tutorial answers that question. Converting a list to integer in Python consists of taking the list of numbers,...
Published: April 3, 2024
🌐
Bobby Hadz
bobbyhadz.com › blog › python-split-integer-into-digits
How to Split an Integer into Digits in Python | bobbyhadz
Copied!an_int = 13579 list_of_digits ... # [9, 7, 5, 3, 1] ... Notice that the result is produced from right to left. The divmod() function takes two numbers and returns a tuple containing 2 values:...
🌐
Medium
medium.com › @aleksej.gudkov › how-to-convert-numbers-in-a-list-using-the-int-function-e20425dface6
How to Convert Numbers in a List Using the int() Function | by UATeam | Medium
November 24, 2024 - Python’s built-in int() function makes this conversion simple and efficient. In this article, you’ll learn how to use the int() function to convert elements in a list to integers.
🌐
Python Help
pythonhelp.org › tutorials › convert-list-to-int
How to convert a list to int in Python
September 23, 2024 - Converting a list to an integer in Python is a common task that can be accomplished using various methods. By understanding the techniques and best practices outlined in this guide, you can efficiently perform this conversion in your Python projects. Whether you’re using loops, join(), or reduce(), each method offers unique advantages that can be leveraged based on your specific needs.
Top answer
1 of 12
106

Convert the integer to string first, and then use map to apply int on it:

>>> num = 132
>>> map(int, str(num))    #note, This will return a map object in python 3.
[1, 3, 2]

or using a list comprehension:

>>> [int(x) for x in str(num)]
[1, 3, 2]
2 of 12
24

There are already great methods already mentioned on this page, however it does seem a little obscure as to which to use. So I have added some mesurements so you can more easily decide for yourself:


A large number has been used (for overhead) 1111111111111122222222222222222333333333333333333333

Using map(int, str(num)):

import timeit

def method():
    num = 1111111111111122222222222222222333333333333333333333
    return map(int, str(num))

print(timeit.timeit("method()", setup="from __main__ import method", number=10000)

Output: 0.018631496999999997

Using list comprehension:

import timeit

def method():
    num = 1111111111111122222222222222222333333333333333333333
    return [int(x) for x in str(num)]

print(timeit.timeit("method()", setup="from __main__ import method", number=10000))

Output: 0.28403817900000006

Code taken from this answer

The results show that the first method involving inbuilt methods is much faster than list comprehension.

The "mathematical way":

import timeit

def method():
    q = 1111111111111122222222222222222333333333333333333333
    ret = []
    while q != 0:
        q, r = divmod(q, 10) # Divide by 10, see the remainder
        ret.insert(0, r) # The remainder is the first to the right digit
    return ret

print(timeit.timeit("method()", setup="from __main__ import method", number=10000))

Output: 0.38133582499999996

Code taken from this answer

The list(str(123)) method (does not provide the right output):

import timeit

def method():
    return list(str(1111111111111122222222222222222333333333333333333333))
    
print(timeit.timeit("method()", setup="from __main__ import method", number=10000))

Output: 0.028560138000000013

Code taken from this answer

The answer by Duberly González Molinari:

import timeit

def method():
    n = 1111111111111122222222222222222333333333333333333333
    l = []
    while n != 0:
        l = [n % 10] + l
        n = n // 10
    return l

print(timeit.timeit("method()", setup="from __main__ import method", number=10000))

Output: 0.37039988200000007

Code taken from this answer

Remarks:

In all cases the map(int, str(num)) is the fastest method (and is therefore probably the best method to use). List comprehension is the second fastest (but the method using map(int, str(num)) is probably the most desirable of the two.

Those that reinvent the wheel are interesting but are probably not so desirable in real use.