Normalize a List Practice Problem
This data science coding problem helps you practice List & Dict Comprehensions, normalize a list, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of List & Dict Comprehensions.
- Problem ID: 94
- Problem key: 94-normalize-a-list
- URL: https://datacrack.app/solve/94-normalize-a-list
- Difficulty: easy
- Topic: List & Dict Comprehensions
- Module: Python Fundamentals
Problem Statement
# 🧩 Normalize a List (Min-Max Scaling)
---
### 🎯 Goal
Min-max normalization (also called **feature scaling**) rescales values to the range [0, 1]. It's one of the most common preprocessing steps in machine learning — required before feeding data to algorithms sensitive to feature magnitudes (KNN, SVM, neural networks).
$$
x_{\text{norm}} = \frac{x - x_{\min}}{x_{\max} - x_{\min}}
$$
This problem uses a **list comprehension** to apply this formula to every element.
---
### 💻 Task
Implement `normalize(numbers)` using a list comprehension.
---
### 📥 Input
- `numbers`: list of numbers — at least 2 elements, not all identical
### 📤 Output
- A list of floats in the range [0.0, 1.0], in the same order
---
### 🧩 Starter Code
```python
def normalize(numbers):
"""
Apply min-max normalization to a list of numbers.
Args:
numbers (list): List of numbers
Returns:
list[float]: Normalized values in [0, 1]
"""
min_val = min(numbers)
max_val = max(numbers)
# 🧠 TODO: Use a list comprehension to apply the formula to each element
pass
```
---
### 💡 Example
```python
normalize([0, 5, 10])
# Expected: [0.0, 0.5, 1.0]
normalize([1, 2, 3, 4, 5])
# Expected: [0.0, 0.25, 0.5, 0.75, 1.0]
```
---
### 🔑 Key Concepts
- `min()` and `max()` are allowed here (the focus is on comprehension syntax)
- The result is always `[0.0, ..., 1.0]` — min maps to 0, max maps to 1
- This will be replicated using NumPy in the NumPy moduleStarter Code
def normalize(numbers):
"""
Apply min-max normalization to a list of numbers.
Args:
numbers (list): List of numbers
Returns:
list[float]: Normalized values in [0, 1]
"""
min_val = min(numbers)
max_val = max(numbers)
# 🧠 TODO: Use a list comprehension to apply the formula to each element
pass