The Wayback Machine - https://web.archive.org/web/20221108012844/https://www.geeksforgeeks.org/python-convert-tuple-to-integer/
Skip to content
Related Articles

Related Articles

Python | Convert Tuple to integer

View Discussion
Improve Article
Save Article
  • Last Updated : 19 Sep, 2022
View Discussion
Improve Article
Save Article

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




# 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




# 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

Method #3: Using str() and int() methods

Python3




# Python3 code to demonstrate working of
# Convert Tuple to integer
 
# initialize tuple
test_tuple = (1, 4, 5)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Convert Tuple to integer
res=""
for i in test_tuple:
    res+=str(i)
res=int(res)
 
# printing result
print("Tuple to integer conversion : " + str(res))

Output

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

Method: Using in operator + end() function

Python3




test_tuple=(1,4,5)
for i in test_tuple:
  print(i,end="")

Output

145

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!