How to convert a Tuple to an Integer in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Convert a Tuple to an Integer in Python
  2. Convert a Tuple to an Integer using reduce()
  3. Convert a Tuple to an Integer using join()
  4. Convert a Tuple to an Integer with the sum() function
  5. Convert a tuple to an integer using a for loop
  6. Convert a Tuple of Strings to a Tuple of Integers in Python
  7. Convert a Tuple of Strings to a Tuple of Integers using map()

# Convert a tuple to an integer in Python

There are multiple ways to convert a tuple to an integer:

  1. Access a tuple element at its index and convert it to an int, e.g. int(my_tuple[0]).
  2. Sum or multiply the elements of the tuple.
  3. Convert a tuple of strings to a tuple of integers.
main.py
# โœ… Access the tuple element and convert it to an integer a_tuple = ('1', '3', '5') an_int = int(a_tuple[0]) print(an_int) # ๐Ÿ‘‰๏ธ 1

convert tuple to integer

The code for this article is available on GitHub

If you need to sum the elements of a tuple to get an integer, use the following code sample instead.

main.py
# โœ… Sum the elements of a tuple to get an integer a_tuple = (2, 4, 6) an_int = sum(a_tuple) print(an_int) # ๐Ÿ‘‰๏ธ 12

sum elements of tuple

The first example accesses the tuple element at a specific index and uses the int() class to convert it to an integer.

main.py
a_tuple = ('1', '3', '5') an_int = int(a_tuple[0]) print(an_int) # ๐Ÿ‘‰๏ธ 1
Python indexes are zero-based, so the first element in a tuple has an index of 0, the second an index of 1, etc.

When the index starts with a minus, we start counting backward from the end of the tuple.

For example, the index -1 gives us access to the last element, -2 to the second-last, etc.

main.py
my_tuple_1 = ('1', '3', '5') my_integer = int(my_tuple_1[-1]) print(my_integer) # ๐Ÿ‘‰๏ธ 5

You only have to use the int() class if your tuple doesn't store integers.

Otherwise, directly access the tuple element at its index.

main.py
my_tuple_1 = (1, 3, 5) my_integer = my_tuple_1[1] print(my_integer) # ๐Ÿ‘‰๏ธ 3

# Convert a tuple to an integer using reduce()

You can also use the reduce() function from the functools module to convert a tuple to an integer.

main.py
from functools import reduce a_tuple = (2, 4, 6) result = reduce( lambda accumulator, current: accumulator * 2 + current, a_tuple) print(result) # ๐Ÿ‘‰๏ธ 22

convert tuple to integer using reduce

The code for this article is available on GitHub

The reduce() function takes the following 3 parameters:

NameDescription
functionA function that takes 2 parameters - the accumulated value and a value from the iterable.
iterableEach element in the iterable will get passed as an argument to the function.
initializerAn optional initializer value that is placed before the items of the iterable in the calculation.

The lambda function we passed to the reduce() gets called with the accumulated value and the current tuple element.

The code sample multiplies the accumulator by two and adds the current tuple element to the result.

main.py
from functools import reduce a_tuple = (2, 4, 6) result = reduce( lambda accumulator, current: accumulator * 2 + current, a_tuple) print(result) # ๐Ÿ‘‰๏ธ 22

# Convert a tuple to an integer using join()

You can also use the str.join() method to convert a tuple to an integer.

main.py
a_tuple = (2, 4, 6) result = int(''.join(map(str, a_tuple))) print(result) # ๐Ÿ‘‰๏ธ 246

convert tuple to integer using join

The code for this article is available on GitHub

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

We passed the str() class to the map() function to convert each element of the tuple to a string.

The next step is to use the str.join() method to join the tuple into a string.

main.py
a_tuple = (2, 4, 6) print(''.join(map(str, a_tuple))) # ๐Ÿ‘‰๏ธ "246"

The last step is to convert the string to an integer with the int() class.

# Convert a tuple to an integer with the sum() function

You can also convert a tuple to an integer by using the sum() function.

main.py
a_tuple = (2, 4, 6) result = sum(a_tuple) print(result) # ๐Ÿ‘‰๏ธ 12

convert tuple to integer using sum function

The code for this article is available on GitHub

The sum() function takes an iterable, sums its items from left to right and returns the total.

main.py
my_list = [10, 20, 30] print(sum(my_list)) # ๐Ÿ‘‰๏ธ 60

# Convert a tuple to an integer using a for loop

You can also use a for loop to convert a tuple to an integer.

main.py
a_tuple = (2, 4, 6) result = '' for number in a_tuple: result += str(number) result = int(result) print(result) # ๐Ÿ‘‰๏ธ 246

We declared a new variable that stores an empty string and used a for loop to iterate over the tuple.

On each iteration, we convert the current tuple element to a string and append it to the result.

The last step is to convert the result string to an integer with the int() class.

# Convert a tuple of strings to a tuple of integers in Python

To convert a tuple of strings to a tuple of integers:

  1. Use a generator expression to iterate over the tuple.
  2. Convert each tuple item to an integer by passing them to the int() class.
  3. Use the tuple() class to convert the generator object to a tuple.
main.py
tuple_of_strings = ('10', '20', '30', '40') tuple_of_integers = tuple(int(item) for item in tuple_of_strings) # ๐Ÿ‘‡๏ธ (10, 20, 30, 40) print(tuple_of_integers)
The code for this article is available on GitHub

We used a generator expression to iterate over the tuple of strings.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we pass the tuple element to the int() class to convert it to an integer.

The int class returns an integer object constructed from the provided number or string argument.

Alternatively, you can use the map() function.

# Convert a tuple of strings to a tuple of integers using map()

This is a three-step process:

  1. Pass the int() class to the map() function.
  2. The map() function will pass each item of the tuple to the int() class.
  3. The new tuple will only contain integer values.
main.py
tuple_of_strings = ('10', '20', '30', '40') tuple_of_integers = tuple(map(int, tuple_of_strings)) # ๐Ÿ‘‡๏ธ (10, 20, 30, 40) print(tuple_of_integers)
The code for this article is available on GitHub

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

This approach is a bit more implicit than using a generator expression.

The map() function returns a map object, so we had to use the tuple() class to convert the map object to a tuple.

Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

Since tuples cannot be changed, the only way to convert a tuple of strings to a tuple of integers is to create a new tuple that contains integer values.

Which approach you pick is a matter of personal preference. I'd go with the generator expression as I find it more direct and more explicit.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev