Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Convert List to Tuple in Python
Next article icon

Python | Convert Tuple to integer

Last Updated : 16 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Time complexity: O(n)
Auxiliary space: O(1)

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

Time complexity: O(n), where n is the length of the input tuple.
O(k), where k is the length of the resulting string.

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 #4: Using in operator + end() function

Python3




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

Method #5 : Using regular expressions (re module)
This method uses the re module to match the elements of the tuple as individual digits and then join them using the re.sub() method. The resulting string is then converted to an integer using the int() method.

Python3




import re
 
test_tuple = (1, 4, 5)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Convert Tuple to integer using regular expressions
res = int(re.sub(r'\D', '', ''.join(map(str, test_tuple))))
 
# printing result
print("Tuple to integer conversion : " + str(res))
 
#this code is contributed by edula vinay kumar reddy
 
 
Output
The original tuple : (1, 4, 5) Tuple to integer conversion : 145

Time complexity: O(n)
Auxiliary Space: O(n)

Method#6: Using Recursive method.

The algorithm of the tuple_to_int() function is as follows:

  1. If the length of the tuple is 1, return the only element in the tuple.
  2. Otherwise, take the first element of the tuple and multiply it by 10 to the power of the length of the tuple minus 1.
  3. Add the result to the recursive call to tuple_to_int() with the remaining elements of the tuple.
  4. Repeat until the base case is reached.

Python3




def tuple_to_int(tup):
    if len(tup) == 1:
        return tup[0]
    else:
        return tup[0] * (10 ** (len(tup) - 1)) + tuple_to_int(tup[1:])
 
test_tuple = (1, 4, 5)
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
res = tuple_to_int(test_tuple)
# printing result
print("Tuple to integer conversion : " + str(res))
  
#this code is contributed by tvsk.
 
 
Output
The original tuple : (1, 4, 5) Tuple to integer conversion : 145

Time complexity: O(n), where n is the length of the tuple, because it needs to process each element of the tuple exactly once. 
Auxiliary space: O(n), because the function call stack can have at most n function, calls in it at any given time.

Method #7: Using math.pow() + reversed()

  • Create a function tuple_to_int that takes a tuple as input.
  • Use the enumerate() function along with the reversed() function to iterate through the tuple in reverse order and assign an index to each element.
  • For each element in the reversed tuple, calculate the power of 10 to the index using the math.pow() function, and multiply it with the element.
  • Use the sum() function to add up all the calculated values to get the final integer value.
  • Create a tuple test_tuple with values (1, 4, 5).
  • Print the original tuple using the print() function.
  • Call the tuple_to_int() function with test_tuple as the argument and assign the result to a variable res.
  • Print the result using the print() function.

Python3




import math
 
def tuple_to_int(tup):
    return sum(math.pow(10, i) * num for i, num in enumerate(reversed(tup)))
 
test_tuple = (1, 4, 5)
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
res = tuple_to_int(test_tuple)
# printing result
print("Tuple to integer conversion : " + str(res))
 
 
Output
The original tuple : (1, 4, 5) Tuple to integer conversion : 145.0

Time complexity: O(n)
Auxiliary space: O(1)



Next Article
Convert List to Tuple in Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python tuple-programs
Practice Tags :
  • python

Similar Reads

  • Python - Convert Binary tuple to Integer
    Given Binary Tuple representing binary representation of a number, convert to integer. Input : test_tup = (1, 1, 0) Output : 6 Explanation : 4 + 2 = 6. Input : test_tup = (1, 1, 1) Output : 7 Explanation : 4 + 2 + 1 = 7. Method #1 : Using join() + list comprehension + int() In this, we concatenate t
    5 min read
  • Convert List to Tuple in Python
    The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like
    2 min read
  • Python - Convert Tuple String to Integer Tuple
    Interconversion of data is a popular problem developer generally deal with. One can face a problem to convert tuple string to integer tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + int() + replace() + split() The combination of above methods can be
    7 min read
  • Python | Convert Integral list to tuple list
    Sometimes, while working with data, we can have a problem in which we need to perform type of interconversions of data. There can be a problem in which we may need to convert integral list elements to single element tuples. Let's discuss certain ways in which this task can be performed. Method #1 :
    3 min read
  • Convert tuple to string in Python
    The goal is to convert the elements of a tuple into a single string, with each element joined by a specific separator, such as a space or no separator at all. For example, in the tuple ('Learn', 'Python', 'Programming'), we aim to convert it into the string "Learn Python Programming". Let's explore
    3 min read
  • Python - Convert Tuple to Tuple Pair
    Sometimes, while working with Python Tuple records, we can have a problem in which we need to convert Single tuple with 3 elements to pair of dual tuple. This is quite a peculiar problem but can have problems in day-day programming and competitive programming. Let's discuss certain ways in which thi
    10 min read
  • Python | Convert String to tuple list
    Sometimes, while working with Python strings, we can have a problem in which we receive a tuple, list in the comma-separated string format, and have to convert to the tuple list. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + split() + replace() This is a br
    5 min read
  • Python | Convert list to indexed tuple list
    Sometimes, while working with Python lists, we can have a problem in which we need to convert a list to tuple. This kind of problem have been dealt with before. But sometimes, we have it's variation in which we need to assign the index of the element along with element as a tuple. Let's discuss cert
    3 min read
  • Python | Convert tuple to float value
    Sometimes, while working with tuple, we can have a problem in which, we need to convert a tuple to floating-point number in which first element represents integer part and next element represents a decimal part. Let's discuss certain way in which this can be achieved. Method : Using join() + float()
    3 min read
  • Convert Tuple to Json Array in Python
    Python's versatility as a programming language extends to its rich data structures, including tuples and JSON. JSON, abbreviation for JavaScript Object Notation, is a lightweight data format used for representing structured data. Moreover, it is a syntax for storing and exchanging data. In this arti
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences