Nested Lists Basics Practice Problem
This data science coding problem helps you practice Lists & Basic Data Structures, nested lists basics, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Lists & Basic Data Structures.
- Problem ID: 73
- Problem key: 73-nested-lists-basics
- URL: https://datacrack.app/solve/73-nested-lists-basics
- Difficulty: easy
- Topic: Lists & Basic Data Structures
- Module: Python Fundamentals
Problem Statement
# 🧩 Nested Lists Basics
---
### 🎯 Goal
A **nested list** (list of lists) is Python's way of representing 2D data — tables, matrices, grids, and spreadsheets. Understanding how to navigate nested lists is the bridge to NumPy arrays and Pandas DataFrames.
---
### 🔍 How Nested Lists Work
```python
matrix = [
[1, 2, 3], # Row 0
[4, 5, 6], # Row 1
[7, 8, 9] # Row 2
]
matrix[0] # [1, 2, 3] — entire row 0
matrix[1][2] # 6 — row 1, column 2
len(matrix) # 3 — number of rows
len(matrix[0]) # 3 — number of columns
```
> 💡 Think of it as: **first index = row**, **second index = column**.
---
### 💻 Task
Implement `explore_matrix(matrix, row, col)` that returns a dictionary with:
- `element`: the value at `matrix[row][col]`
- `row_data`: the entire row at `matrix[row]`
- `num_rows`: total number of rows
- `num_cols`: number of columns (using the first row)
---
### 📥 Input
- `matrix`: list of lists (a 2D grid, guaranteed rectangular)
- `row`: int — the row index
- `col`: int — the column index
### 📤 Output
- A dictionary: `{"element": ..., "row_data": ..., "num_rows": ..., "num_cols": ...}`
---
### 🧩 Starter Code
```python
def explore_matrix(matrix, row, col):
"""
Extract information from a 2D matrix (list of lists).
Args:
matrix (list[list]): A 2D grid of values
row (int): Row index
col (int): Column index
Returns:
dict: Dictionary with 'element', 'row_data', 'num_rows', 'num_cols'
"""
# 🧠 TODO: Access the element at [row][col]
# 🧠 TODO: Get the entire row
# 🧠 TODO: Count rows and columns
pass
```
---
### 💡 Example
```python
explore_matrix([[1,2,3],[4,5,6],[7,8,9]], 1, 2)
# Expected: {"element": 6, "row_data": [4, 5, 6], "num_rows": 3, "num_cols": 3}
```
---
### 🔑 Key Concepts
- `matrix[row]` returns a list (the entire row)
- `matrix[row][col]` chains two index operations to get a single element
- `len(matrix)` = number of rows, `len(matrix[0])` = number of columns
- This is the same `[row, col]` convention used by NumPy: `array[row, col]`Starter Code
def explore_matrix(matrix, row, col):
"""
Extract information from a 2D matrix (list of lists).
Args:
matrix (list[list]): A 2D grid of values
row (int): Row index
col (int): Column index
Returns:
dict: Dictionary with 'element', 'row_data', 'num_rows', 'num_cols'
"""
# 🧠 TODO: Access the element at [row][col]
# 🧠 TODO: Get the entire row
# 🧠 TODO: Count rows and columns
pass