Given a list of integers, write a Python program to convert the given list into a single integer.
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.
Approach #2 : Using
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
Approach #3 : Using
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.
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
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.
- Python | Convert list of string into sorted list of integer
- Python | Convert string list into multiple cases
- Python - Convert list to Single Dictionary Key Value list
- Combine Multiple Excel Worksheets Into a Single Pandas Dataframe
- Python | Split a list having single integer
- Python | Convert list of numerical string to list of Integers
- Python Program to convert List of Integer to List of String
- Python - Group single item dictionaries into List values
- Python | Convert a nested list into a flat list
- Python | Convert a string representation of list into list
- Python | Convert list of tuples into list
- Python | Convert given list into nested list
- Python | Convert list into list of lists
- Python | Ways to Convert a 3D list into a 2D list
- Python | Convert String list to Joined Single element
- Python - Convert List to Single valued Lists in Tuple
- Python - Integers String to Integer List
- Print Single and Multiple variable in Python
- Python | Replace multiple occurrence of character by single
- Python | Convert number to list of integers