- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Tuple to integer in Python
When it is required to convert a tuple into an integer, the lambda function and the 'reduce' function can be used.
Anonymous function is a function which is defined without a name. The reduce function takes two parameters- a function and a sequence, where it applies the function to all the elements of the list/sequence. It is present in the 'functools' module.
In general, functions in Python are defined using 'def' keyword, but anonymous function is defined with the help of 'lambda' keyword. It takes a single expression, but can take any number of arguments. It uses the expression and returns the result of it.
Below is a demonstration of the same −
import functools my_tuple_1 = (23, 45, 12, 56, 78, 0) print("The first tuple is : ") print(my_tuple_1) my_result = functools.reduce(lambda sub, elem: sub * 10 + elem, my_tuple_1) print("After converting tuple to integer, it is ") print(my_result)
The first tuple is : (23, 45, 12, 56, 78, 0) After converting tuple to integer, it is 2768380
- The required packages are downloaded.
- A tuple is defined, and is displayed on the console.
- The reduce function is used, to which the lambda, and the tuple are passed as arguments.
- The lambda function multiples every element in the tuple with 10 and adds the previous element to it.
- This operation's data is stored in a variable.
- This variable is the output that is displayed on the console.
- Convert String to Tuple in Python
- Convert tuple to float value in Python
- Convert location coordinates to tuple in Python
- How to convert float to integer in Python?
- Convert tuple to adjacent pair dictionary in Python
- Python program to convert Set into Tuple and Tuple into Set
- Program to convert roman numeral to integer in Python?
- Program to convert integer to roman numeral in Python
- Convert Nested Tuple to Custom Key Dictionary in Python
- Python – Convert Integer Matrix to String Matrix
- How to convert an integer to a character in Python?
- Convert a list into a tuple in Python.
- How can I convert a Python tuple to string?
- How to convert JSON data into a Python tuple?
- How can I convert Python tuple to C array?