Access List Elements Practice Problem
This data science coding problem helps you practice Lists & Basic Data Structures, access list elements, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Lists & Basic Data Structures.
- Problem ID: 67
- Problem key: 67-access-list-elements
- URL: https://datacrack.app/solve/67-access-list-elements
- Difficulty: easy
- Topic: Lists & Basic Data Structures
- Module: Python Fundamentals
Problem Statement
# 🧩 Access List Elements
---
### 🎯 Goal
Once you have a list, the next skill is **reading individual elements** by their position. Python uses **zero-based indexing** and supports **negative indices** to access elements from the end — a feature you'll use constantly in data science.
---
### 🔍 How Indexing Works
| Index | Element (for `[10, 20, 30, 40, 50]`) |
|:------|:--------------------------------------|
| `0` | `10` (first) |
| `1` | `20` (second) |
| `-1` | `50` (last) |
| `-2` | `40` (second to last) |
> ⚠️ Accessing an index outside the list (e.g., index `5` in a 5-element list) raises an `IndexError`.
---
### 💻 Task
Implement `access_element(lst, index)` that returns the element at the given index.
---
### 📥 Input
- `lst`: list — the list to access
- `index`: int — the position to retrieve (can be negative)
### 📤 Output
- The element at that index
---
### 🧩 Starter Code
```python
def access_element(lst, index):
"""
Return the element at the given index in the list.
Args:
lst (list): The input list
index (int): The index to access (supports negative indexing)
Returns:
The element at the specified index
"""
# 🧠 TODO: Return the element at the given index
pass
```
---
### 💡 Example
```python
access_element([10, 20, 30, 40, 50], 0)
# Expected: 10
access_element([10, 20, 30, 40, 50], -1)
# Expected: 50
```
---
### 🔑 Key Concepts
- `lst[0]` gets the **first** element
- `lst[-1]` gets the **last** element
- Negative indices count backward from the end
- `IndexError` is raised if the index is out of rangeStarter Code
def access_element(lst, index):
"""
Return the element at the given index in the list.
Args:
lst (list): The input list
index (int): The index to access (supports negative indexing)
Returns:
The element at the specified index
"""
# 🧠 TODO: Return the element at the given index
pass