Given a list of integers, write a Python program to convert the given list into a single integer. Examples:
Input : [1, 2, 3]
Output : 123
Input : [55, 32, 890]
Output : 5532890
There are multiple approaches possible to convert the given list into a single integer. Let’s see each one by one. Approach #1 : Naive Method Simply iterate each element in the list and print them without space in between.
Python3
lst = [ 12 , 15 , 17 ]
for i in lst:
print (i, end = "")
|
Output:
121517
Approach #2 : Using join() Use the join() method of Python. First convert the list of integer into a list of strings( as join() works with strings only). Then, simply join them using join() method. It takes a time complexity of O(n).
Python3
def convert( list ):
s = [ str (i) for i in list ]
res = int ("".join(s))
return (res)
list = [ 1 , 2 , 3 ]
print (convert( list ))
|
Output:
123
Approach #3 : Using map() Another approach to convert a list of multiple integers into a single integer is to use map() function of Python with str function to convert the Integer list to string list. After this, join them on the empty string and then cast back to integer.
Python3
def convert( list ):
res = int ("".join( map ( str , list )))
return res
list = [ 1 , 2 , 3 ]
print (convert( list ))
|
Output:
123
Approach #4 : Multiplying by corresponding power of 10 A more mathematical way, which does not require to convert the integer list to string list is, to multiply each integer element with its corresponding power of 10, and then summing it up. It takes a time complexity of O(n).
Python3
def convert( list ):
res = sum (d * 10 * * i for i, d in enumerate ( list [:: - 1 ]))
return (res)
list = [ 1 , 2 , 3 ]
print (convert( list ))
|
Output:
123
Method: Using list comprehension
Python3
lst = [ 1 , 2 , 3 ]
s = [ str (i) for i in lst]
print ("".join(s))
|
A small variation to this program leads to less computation in calculation of sum, i.e. using reduce(). This makes use of Horner’s rule, which factors the polynomial representing the number to reduce the number of multiplications.
Python3
res = functools. reduce ( lambda total, d: 10 * total + d, list , 0 )
|
Using list comprehension and the reduce method:
This approach first converts the elements of the list to strings using list comprehension, then concatenates the strings using the reduce function and a lambda function, and finally casts the resulting concatenated string back to an integer before returning it.
Python3
from functools import reduce
def convert(lst):
concat_lst = [ str (i) for i in lst]
res = reduce ( lambda x, y: x + y, concat_lst)
return int (res)
lst = [ 1 , 2 , 3 ]
print (convert(lst))
|
The time complexity of the approach using list comprehension and the reduce method is O(n), where n is the length of the input list. This is because the reduce function iterates through the elements of the list once and performs a constant number of operations on each element.
The space complexity of this approach is also O(n), because a new list of strings is created and a new string is created to store the concatenated elements. In addition, the reduce function may require additional space to store intermediate results during the reduction process.
Note that the specific time and space complexity of this approach may depend on the implementation details of the particular Python version and environment being used.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Dec, 2022
Like Article
Save Article