Python | Convert a list of multiple integers into a single integer
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 program to convert a list # of integers into a single integer # creating a list lst = [12, 15, 17] # iterating each element 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 program to convert a list # of integers into a single integer def convert(list): # Converting integer list to string list s = [str(i) for i in list] # Join list items using join() res = int("".join(s)) return(res) # Driver code 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 program to convert a list # of integers into a single integer def convert(list): # Converting integer list to string list # and joining the list using join() res = int("".join(map(str, list))) return res # Driver code 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 program to convert a list # of integers into a single integer def convert(list): # multiply each integer element with its # corresponding power and perform summation res = sum(d * 10**i for i, d in enumerate(list[::-1])) return(res) # Driver code list = [1, 2, 3] print(convert(list)) |
Output:
123
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.
res = functools.reduce(lambda total, d: 10 * total + d, list, 0) |
Recommended Posts:
- Python | Split a list having single integer
- Python | Convert list of string into sorted list of integer
- Python | Convert number to list of integers
- Print Single and Multiple variable in Python
- Python | Replace multiple occurrence of character by single
- Python | Associating a single value with all list items
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Python | Convert a nested list into a flat list
- Python | Convert list of tuples to list of strings
- Python | Convert a string representation of list into list
- Python | Add similar value multiple times in list
- Remove multiple elements from a list in Python
- Python | Convert list of tuples into list
- Python | Assign multiple variables with list values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



