Skip to content
Related Articles

Related Articles

Improve Article

Python | Convert Tuple to integer

  • Last Updated : 25 Nov, 2019
Geek Week

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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :