
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can I subtract tuple of tuples from a tuple in Python?
The direct way to subtract tuple of tuples from a tuple in Python is to use loops directly. For example, if
you have a tuple of tuples
Example
((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14))
and want to subtract (1, 2, 3, 4, 5) from each of the inner tuples, you can do it as follows
my_tuple = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14)) sub = (1, 2, 3, 4, 5) tuple(tuple(x - sub[i] for x in my_tuple[i]) for i in range(len(my_tuple)))
Output
This will give the output
((-1, 0, 1), (1, 2, 3), (3, 4, 5), (5, 6, 7), (7, 8, 9))
- Related Articles
- How can I append a tuple into another tuple in Python?
- How can I do Python Tuple Slicing?
- How can I use Multiple-tuple in Python?
- How can I create a Python tuple of Unicode strings?
- How can I remove items out of a Python tuple?
- How we can use Python Tuple within a Tuple?
- How can I create a non-literal python tuple?
- How I can convert a Python Tuple into Dictionary?
- How can I convert a Python tuple to string?
- How can I define duplicate items in a Python tuple?
- How I can dump a Python tuple in JSON format?
- Reverse each tuple in a list of tuples in Python
- How can I convert Python strings into tuple?
- How can I represent python tuple in JSON format?
- How can I convert a Python tuple to an Array?

Advertisements