Write a program to turn multiple integers into a single integer using Python

In this article, TipsMake.com will learn how to write a program to turn multiple integers into a single integer using the Python programming language.

Problem: Given a list of many integers, write a Python program to turn the given list into a single integer.

For example:

Input : [1, 2, 3] Output : 123 Input : [55, 32, 890] Output : 5532890

In this article, TipsMake.com will learn how to write a program to turn multiple integers into a single integer using the Python programming language.

There are quite a few approaches available for turning multiple integers into a single integer. Below, we will explore each of them one by one.

Method 1: The most basic method

You just iterate over each element in the list and return them without spaces in between.

Here is the sample code for your reference:

# 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="")

The returned result is:

121517

Method 2: Use join()

The second approach is to use Python's join() method. First, you need to convert the list of integers to a string (the reason is because join() only works with strings.Next, you simply need to use the join() method to combine them together and return them. about results.

Here is the sample code for your reference:

# 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))

The returned result is:

123

Method 3: Use map()

The next approach to convert a list of multiple integers to a single integer is to use Python's map() function with the str function to convert a list of integers to a list of strings. Then combine them together on an empty string and finally return the combined integer.

Here is sample code:

# 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))

The returned result is:

123

Method 4: Multiply each integer by the power of 10

This is a more mathematical approach, and it doesn't require you to convert a list of integers to a list of strings. You need to multiply each integer element by the corresponding power of 10 and then calculate the sum.

Here is the sample code for your reference:

# 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))

The returned result is:

123

TipsMake.com hopes that this article will be useful to you.

3.5 ★ | 2 Vote