Find Length, Sum, Min, Max Practice Problem
This data science coding problem helps you practice Lists & Basic Data Structures, find length, sum, min, max, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Lists & Basic Data Structures.
- Problem ID: 71
- Problem key: 71-find-length-sum-min-max
- URL: https://datacrack.app/solve/71-find-length-sum-min-max
- Difficulty: easy
- Topic: Lists & Basic Data Structures
- Module: Python Fundamentals
Problem Statement
# 🧩 Find Length, Sum, Min, Max
---
### 🎯 Goal
Python provides powerful **built-in functions** that work directly on lists. Before reaching for Pandas or NumPy, you should know these core tools — they're fast, readable, and available everywhere without any imports.
---
### 🔍 Built-in Aggregate Functions
| Function | What It Does | Example |
|:---------|:-------------|:--------|
| `len(lst)` | Number of elements | `len([1,2,3])` → `3` |
| `sum(lst)` | Sum of all elements | `sum([1,2,3])` → `6` |
| `min(lst)` | Smallest element | `min([3,1,2])` → `1` |
| `max(lst)` | Largest element | `max([3,1,2])` → `3` |
> ⚠️ `sum()`, `min()`, and `max()` raise errors on empty lists. `len()` returns `0`.
---
### 💻 Task
Implement `list_stats(lst)` that returns a dictionary with four keys: `length`, `total`, `minimum`, and `maximum`.
---
### 📥 Input
- `lst`: list of numbers (guaranteed non-empty)
### 📤 Output
- A dictionary: `{"length": ..., "total": ..., "minimum": ..., "maximum": ...}`
---
### 🧩 Starter Code
```python
def list_stats(lst):
"""
Compute basic statistics for a list of numbers.
Args:
lst (list): A non-empty list of numbers
Returns:
dict: Dictionary with keys 'length', 'total', 'minimum', 'maximum'
"""
# 🧠 TODO: Use len(), sum(), min(), max() and return as a dict
pass
```
---
### 💡 Example
```python
list_stats([3, 1, 4, 1, 5, 9, 2, 6])
# Expected: {"length": 8, "total": 31, "minimum": 1, "maximum": 9}
list_stats([10])
# Expected: {"length": 1, "total": 10, "minimum": 10, "maximum": 10}
```
---
### 🔑 Key Concepts
- `len()` works on any sequence (lists, strings, tuples, dicts)
- `sum()` only works on numeric lists (not strings)
- `min()` and `max()` use `<` comparison — they also work on strings (alphabetical order)
- These functions are **O(n)** — they scan the entire list onceStarter Code
def list_stats(lst):
"""
Compute basic statistics for a list of numbers.
Args:
lst (list): A non-empty list of numbers
Returns:
dict: Dictionary with keys 'length', 'total', 'minimum', 'maximum'
"""
# 🧠 TODO: Use len(), sum(), min(), max() and return as a dict
pass