
- 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
Remove nested records from tuple in Python
When it is required to remove nested record/tuples from a tuple of tuple, a simple loop and the 'isinstance' method and the enumerate method can be used.
The enumerate method adds a counter to the given iterable, and returns it. The 'isinstance' method checks to see if a given parameter belong to a specific data type or not.
Below is a demonstration of the same −
Example
tuple_1 = (11, 23, (41, 25, 22), 19) print("The tuple is : ") print(tuple_1) my_result = tuple() for count, elem in enumerate(tuple_1): if not isinstance(elem, tuple): my_result = my_result + (elem, ) print("Elements after removing the nested tuple is : ") print(my_result)
Output
The tuple is : (11, 23, (41, 25, 22), 19) Elements after removing the nested tuple is : (11, 23, 19)
Explanation
- A tuple is defined, and is displayed on the console.
- Another empty tuple is defined.
- The first tuple is enumerated, and iterated over.
- If the element inside the tuple isn't an instance of a specific type, that element is added to the empty list.
- This operation is assigned to a variable.
- It is displayed as the output on the console.
- Related Articles
- Find minimum k records from tuple list in Python
- Cummulative Nested Tuple Column Product in Python
- Intersection in Tuple Records Data in Python
- Convert Nested Tuple to Custom Key Dictionary in Python
- How to get unique elements in nested tuple in Python
- Remove tuple from list of tuples if not containing any character in Python
- Python program to Flatten Nested List to Tuple List
- Remove similar element rows in tuple Matrix in Python
- Remove/ filter duplicate records from array - JavaScript?
- Remove item from a nested array by indices in JavaScript
- Removing duplicates from tuple in Python
- Removing strings from tuple in Python
- Finding unique elements from Tuple in Python
- How can I subtract tuple of tuples from a tuple in Python?
- How can I remove items out of a Python tuple?

Advertisements