In [8]: import numpy as np

In [9]: a = np.array([1,2,3,4,5])

In [10]: int("".join(map(str, a)))
Out[10]: 12345
Answer from Akavall on Stack Overflow
Top answer
1 of 5
4
In [8]: import numpy as np

In [9]: a = np.array([1,2,3,4,5])

In [10]: int("".join(map(str, a)))
Out[10]: 12345
2 of 5
3

Just use the sum() function with a generator:

>>> sum(e * 10 ** i for i, e in enumerate(x[::-1]))
12345

or, alternatively:

>>> sum(e * 10 ** (len(x) - i-1) for i, e in enumerate(x))
12345

or you could use str.join, again with a generator:

>>> int(''.join(str(i) for i in x))
12345

why?

In the first example, we use a generator to yield each number multiplied by 10 to the power of its position in the array, we then add these together to get our final result. This may be easier to see if we use a list-comprehension, rather than a generator to see what is going on:

>>> [e * 10 ** i for i, e in enumerate(x[::-1])]
[5, 40, 300, 2000, 10000]

and then it is clear from here that, through summing these numbers together, we will get the result of 12345. Note, however, that we had to reverse x before using this, as otherwise the numbers would be multiplied by the wrong powers of 10 (i.e. the first number would be multiplied by 1 rather than 10000).

In the second snippet, the code simply uses a different method to get the right power of 10 for each index. Rather than reversing the array, we simply subtract the index from the length of x (len(x)), and minus one more so that the first element is 10000, not 100000. This is simply an alternative.

Finally, the last method should be fairly self-explanatory. We are merely joining together the stringified versions of each number in the array, and then converting back to an int to get our result.

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

Discussions

Python: Convert array into integer - Stack Overflow
I was wondering if there is a way in Python to convert an array or a list into one single number: A=[0xaa,0xbb,0xcc] d=0xaabbcc Thanks for your help! More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 9, 2015
python - convert array([[ number ]]) to 'number' - Stack Overflow
Sign up to request clarification or add additional context in comments. ... anywhere you are working with an array (i.e. More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Convert list of ints to one number? - Stack Overflow
The primary value of this is to emulate the behavior of casting a byte array to another data type, e.g. uint16, which Python can't seem to do natively, on either a big or little endian system. ... A pair of non-strs based approaches. Assumed that each term in numList is a natural number smaller ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Convert list or numpy array of single element to float in python - Stack Overflow
Based on the success with the numpy array conversion to float this lead me to the approach: ... And this works when list_ is both a Python list and when it is a numpy array. ... But it seems like this approach has an overhead first converting the list to a numpy array and then to float. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ converting-numpy-array-values-into-integers-a-comprehensive-guide
Converting Numpy Array Values into Integers: A Guide | Saturn Cloud Blog
May 1, 2026 - Numpy provides several functions to convert the data types of numbers. The astype() function is one of the most commonly used functions for this purpose. Letโ€™s dive into how you can use this function to convert Numpy array values into integers.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-convert-data-types-of-arrays-using-numpy-in-python
How to convert data types of arrays using NumPy in Python
We make a copy of the array and use the array.astype() method to change its data type. After doing this, we can easily specify the new data type of the array we want. ... The astype() method takes the typecode value as its only parameter value.
Find elsewhere
๐ŸŒ
Moonbooks
en.moonbooks.org โ€บ Articles โ€บ How-to-convert-a-float-array-to-an-integer-array-in-python-
How to convert a float array to an integer array in python ?
March 22, 2019 - To convert a float array to an integer array in python, a solution is to use astype, example: >>> import numpy as np >>> A = np.array((0.4, 1.6, 2.1, -3.7, 2.9)) >>> A array([ 0.4, 1.6, 2.1, -3.7, 2.9]) >>> A = A.astype(int) >>> A array([ 0, 1, 2, -3, 2]) It is also possible to round the numbers ...
๐ŸŒ
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 - In Python, a list is a versatile and dynamic data type that allows the storage of multiple values in a single variable. Lists are defined by enclosing elements within square brackets [] and separated by commas. ... Converting a list to an integer involves aggregating its elements into a single numerical value.
๐ŸŒ
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 - One such kind of form can be a number in the form of a string that too is a list in the list and we need to segregate it into digit-separated integers. Let's discuss ... We are given a list of integers and our task is to convert each integer into its string representation. 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
๐ŸŒ
Seaborn Line Plots
marsja.se โ€บ home โ€บ programming โ€บ python โ€บ how to convert a float array to an integer array in python with numpy
How to Convert a Float Array to an Integer Array in Python with NumPy
August 20, 2023 - # python convert array to int twod_int = twod.astype(int)Code language: PHP (php) Now, in the output, from both conversion examples above, we can see that the float numbers were rounded down. In some cases, we may want the float numbers to be rounded according to common practice.
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()
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ python โ€บ python array โ€บ convert float array to int array in python
Convert float array to int array in Python - Java2Blog
November 11, 2021 - ... The numpy module has many functions that can round decimal values in an array. The numpy.round_() function performs this and can round floating numbers to a given decimal place.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-convert-numpy-array-like-1-2-3-to-just-one-digit-like-123
How to convert numpy array like [1,2,3] to just one digit like 123 - Quora
Answer (1 of 3): This is a good excuse for โ€˜reduceโ€™. Adding a digit to a sequence of digits generally multiplies the current value by 10 and adds the value of the new digit. Going from 13 to 137 by appending the 7 multiplies the 13 by 10 and adds 7. So you can do: [code]from functools import ...
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-10876.html
Convert element of list to integer(Two dimentional array)
I'm trying to learn arrays(with numpy) in python3 but i am not able to figure out how do i convert the elements of list to integer(in my case i have two dimentional array). I've tried two solutions that i found(its mentioned in the code) but it gives...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_data_types.asp
NumPy Data Types
The astype() function creates a copy of the array, and allows you to specify the data type as a parameter. The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float ...
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to convert numpy array of floats into integers
How to Convert NumPy Array of Floats into Integers
April 3, 2025 - Method 1: Convert Floats to Integers (Rounded Down) rounded_down_integer_array = float_array.astype(int)