How to convert tuple to integer in Python

Sometimes, while working with records, we can have a problem in which we need to convert the data records to integer by joining them. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using reduce() + lambda
The combination of above functions can be used to perform this task. In this, we use lambda function to perform logic of conversion and reduce performs task of iteration and combining result.

 

# Python3 code to demonstrate working of
# Convert Tuple to integer
# Using reduce() + lambda
import functools

# initialize tuple
test_tuple = (1, 4, 5)

# printing original tuple
print("The original tuple : " + str(test_tuple))

# Convert Tuple to integer
# Using reduce() + lambda
res = functools.reduce(lambda sub, ele: sub * 10 + ele, test_tuple)

# printing result
print("Tuple to integer conversion : " + str(res))

Output :

The original tuple : (1, 4, 5)
Tuple to integer conversion : 145

Method #2 : Using int() + join() + map()
The combination of these functions can also be used to perform this task. In this, we convert each element to string using join() and iterate using map(). At last we perform integer conversion.

# Python3 code to demonstrate working of
# Convert Tuple to integer
# Using int() + join() + map()

# initialize tuple
test_tuple = (1, 4, 5)

# printing original tuple
print("The original tuple : " + str(test_tuple))

# Convert Tuple to integer
# Using int() + join() + map()
res = int(''.join(map(str, test_tuple)))

# printing result
print("Tuple to integer conversion : " + str(res))

Output :

The original tuple : (1, 4, 5)
Tuple to integer conversion : 145

 

Submit Your Programming Assignment Details