Update List Elements Practice Problem
This data science coding problem helps you practice Lists & Basic Data Structures, update list elements, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Lists & Basic Data Structures.
- Problem ID: 74
- Problem key: 74-update-list-elements
- URL: https://datacrack.app/solve/74-update-list-elements
- Difficulty: easy
- Topic: Lists & Basic Data Structures
- Module: Python Fundamentals
Problem Statement
# 🧩 Update List Elements
---
### 🎯 Goal
Lists in Python are **mutable** — unlike strings or tuples, you can change individual elements after the list is created. This is a fundamental property that makes lists the go-to data structure for building and transforming data.
---
### 🔍 How Updating Works
```python
fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango" # Now: ["apple", "mango", "cherry"]
fruits[-1] = "grape" # Now: ["apple", "mango", "grape"]
```
> 💡 **Mutability** means the list is changed **in place** — no new list is created.
---
### 💻 Task
Implement `update_element(lst, index, new_value)` that replaces the element at the given index with `new_value` and returns the modified list.
---
### 📥 Input
- `lst`: list — the list to modify
- `index`: int — the position to update (supports negative indexing)
- `new_value`: the replacement value
### 📤 Output
- The modified list with the element at `index` replaced by `new_value`
---
### 🧩 Starter Code
```python
def update_element(lst, index, new_value):
"""
Replace the element at the given index with new_value.
Args:
lst (list): The input list
index (int): The index to update
new_value: The new value to place at that index
Returns:
list: The modified list
"""
# 🧠 TODO: Update the element at the given index and return the list
pass
```
---
### 💡 Example
```python
update_element([1, 2, 3, 4, 5], 2, 99)
# Expected: [1, 2, 99, 4, 5]
update_element([10, 20, 30], -1, 0)
# Expected: [10, 20, 0]
```
---
### 🔑 Key Concepts
- `lst[index] = new_value` performs an **in-place** update
- Lists are **mutable** — you modify the original, not a copy
- Negative indices work for updates just like they do for access
- Strings and tuples do NOT support item assignment — only lists doStarter Code
def update_element(lst, index, new_value):
"""
Replace the element at the given index with new_value.
Args:
lst (list): The input list
index (int): The index to update
new_value: The new value to place at that index
Returns:
list: The modified list
"""
# 🧠 TODO: Update the element at the given index and return the list
pass