Divide each element in a tuple by a number in Python

avatar

Borislav Hadzhiev

Last updated: Jul 11, 2022

banner

Photo from Unsplash

Divide each element in a tuple by a number in Python #

To divide each element in a tuple by a number:

  1. Use a generator expression to iterate over the tuple.
  2. On each iteration, divide the current tuple element by the number.
  3. Use the tuple() class to convert the generator object to a tuple.
main.py
my_tuple = (8, 12, 20) # ✅ divide each element in tuple by number new_tuple = tuple(item / 2 for item in my_tuple) print(new_tuple) # 👉️ (4.0, 6.0, 10.0) # -------------------------------------- # ✅ divide each element in tuple by number using floor division new_tuple_2 = tuple(item // 2 for item in my_tuple) print(new_tuple_2) # 👉️ (4, 6, 10)

We used a generator expression to iterate over the tuple.

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 divide the current number by a different number and return the result.

The last step is to pass the generator object to the tuple() class.

The tuple class takes an iterable and returns a tuple object.

You might also see examples online that use a list comprehension instead of a generator expression.

main.py
my_tuple = (8, 12, 20) # ✅ divide each element in tuple by number new_tuple = tuple([item / 2 for item in my_tuple]) print(new_tuple) # 👉️ (4.0, 6.0, 10.0)

List comprehensions are also used to perform some operation for every element, or select a subset of elements that meet a condition.

However, using a list comprehension is not necessary since we have to convert the list to a tuple anyway.

If you need to divide each element in a tuple by a number without a remainder, use the floor division // operator.

main.py
my_tuple = (8, 12, 20) new_tuple_2 = tuple(item // 2 for item in my_tuple) print(new_tuple_2) # 👉️ (4, 6, 10)

Division / of integers yields a float, while floor division // of integers results in an integer.

The result of using the floor division operator is that of a mathematical division with the floor() function applied to the result.

main.py
my_num = 50 print(my_num / 5) # 👉️ 10.0 (float) print(my_num // 5) # 👉️ 10 (int)

Using floor division is like applying the math.floor method to the result.

The method returns the largest integer less than or equal to the provided number.

main.py
import math result_1 = math.floor(25 / 4) print(result_1) # 👉️ 6 result_2 = 25 / 4 print(result_2) # 👉️ 6.25
Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

The only way to divide each element in a tuple by a number is to create a new tuple that contains the results of the division.

An alternative approach to using a generator expression is to use the map() function.

main.py
my_tuple = (8, 12, 20) new_tuple = tuple(map(lambda item: item / 2, my_tuple)) print(new_tuple) # 👉️ (4.0, 6.0, 10.0)

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

The map() function calls the lambda with each element in the tuple, and the lambda divides the element by 2.

The last step is to use the tuple() class to convert the map object to a tuple.

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.