Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Just as lists can contain other lists, tuples can contain other tuples to construct nested tuples. When an application requires a multi-dimensional structure where neither the dimensions nor the individual values should ever change during runtime, nested tuples are the ideal architectural choice.
Common real-world use cases include fixed database query result sets, spatial 3D coordinate vectors, configuration tables, and immutable grid systems.
+-------------------------------------------------------------------------+
| NESTED TUPLES REAL-WORLD ANALOGY |
+-------------------------------------------------------------------------+
1. THE HISTORICAL DYNASTIC RECORD (Nested Lineage):
- Consider the genealogical register of an Indian royal dynasty.
- The lineage record is permanently carved in stone:
dynasty = (
("King 1", 1526, ("Prince A", "Prince B")),
("King 2", 1556, ("Prince C", "Prince D"))
)
- Generations branch hierarchically, yet history cannot be rewritten!
2. TEHSIL REVENUE LAND RECORD (Immutable Tabular Rows):
- At a rural district land registry (Tehsil office), a survey block
consists of immutable plot boundaries:
cadastral_grid = (
(Plot_101, Owner_A, 2.5_Acres),
(Plot_102, Owner_B, 4.0_Acres),
(Plot_103, Owner_C, 1.8_Acres)
)
- It represents an official, legally tamper-proof snapshot of ownership.
+-------------------------------------------------------------------------+Accessing data in nested tuples uses chained bracket notation [outer_index][inner_index]:
Because each nested element is itself a tuple, you can destructure fields directly within the for loop header:
Tuples can be nested to arbitrary depths, making them ideal for representing 3D vertices, RGB color palettes, or multi-polygon maps:
To convert a 2D tuple-of-tuples into a 1D flat tuple:
| Scenario | ❌ Anti-Pattern | ✅ Pythonic Idiom |
|---|---|---|
| Unpack in Loop | for row in data: name = row[1] | for id, name, val in data: (In-line unpacking) |
| Tamper Proof Table | Using nested lists [[...], [...]] | Using nested tuples ((...), (...)) |
| Multi-Index Syntax | table[row, col] | table[row][col] |
| Single Row Tuple | ((1, 2)) (Outer parentheses ignored!) | ((1, 2),) (Trailing comma for outer tuple) |
| Flattening | Writing multiple loops with += | tuple(x for r in table for x in r) |
+---------------------------------------------------------------------------+ | NESTED TUPLES CHEAT SHEET | +---------------------------------------------------------------------------+ | Concept | Syntax / Usage | |----------------------+----------------------------------------------------| | 2D Definition | matrix = ((1, 2), (3, 4), (5, 6)) | | Single Nested Row | single_nested = ((10, 20),) <-- Mandatory comma | | Cell Retrieval | matrix[row_idx][col_idx] | | Row Retrieval | matrix[row_idx] | | Loop Unpacking | for a, b, c in nested_tuple: | | Flattening to 1D | tuple(x for row in matrix for x in row) | +---------------------------------------------------------------------------+
Without a trailing comma, parentheses are treated as grouping symbols, not a tuple:
grid = (("A", "B"), ("C", "D"), ("E", "F")), what is grid[2][0]?A. "C" B. "D" C. "E" D. "F"
(10, 20)?A. ((10, 20)) B. ((10, 20),) C. [(10, 20)] D. tuple((10, 20))
table[0][1] = 99 on table = ((1, 2), (3, 4))?A. ValueError B. IndexError C. TypeError: 'tuple' object does not support item assignment D. AttributeError
A. ((1, 2), (3, 4)) B. (1, 2, 3, 4) C. [1, 2, 3, 4] D. (1, 3, 2, 4)
A. Nested tuples consume less memory and cannot be accidentally mutated by other functions B. Tuples execute faster arithmetic C. Tuples support automatic encryption D. Lists cannot be nested
Design a satellite tracking registry for ISRO's earth-observation satellites. Each record is stored as an immutable nested tuple: (Satellite_ID, Name, (Apogee_km, Perigee_km, Inclination_deg), Operational_Status). Write functions to display satellite profiles, extract orbital apogee altitudes, calculate average orbital altitude across the constellation, and identify satellites in Sun-Synchronous Orbit (inclination between 96 and 99 degrees).
In the next lesson, we will continue your Python learning journey with Sets Introduction (10: Sets).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Tuple Methods | Sets Introduction |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.