List Operations
Problem Statement
Perform common list operations.
Background
Python lists support many operations:
- Append: lst.append(item)
- Extend: lst.extend([items])
- Insert: lst.insert(index, item)
- Remove: lst.remove(item)
- Pop: lst.pop(index)
- Slice: lst[start:end]
Your Task
Write a function list_stats(numbers) that computes statistics about a list of numbers.
Output Format
Return a dictionary with:
- "length": Number of elements
- "sum": Sum of all elements
- "min": Minimum value
- "max": Maximum value
- "first_three": First 3 elements as list
- "last_three": Last 3 elements as list
- "sorted": Sorted list (ascending)
Example
[5, 2, 8, 1, 9, 3]
{'length': 6, 'sum': 28, 'min': 1, 'max': 9, 'first_three': [5, 2, 8], 'last_three': [1, 9, 3], 'sorted': [1, 2, 3, 5, 8, 9]}- Input:
numbersis a list of six integers,[5, 2, 8, 1, 9, 3]. Positions run to with . - Size and total: there are elements, so
lengthis6. Adding them gives , thesum. - Extremes: the smallest is (position 4) and the largest is (position 5), so
minis1andmaxis9. - Portions:
first_threetakes positions –, giving[5, 2, 8];last_threetakes positions to , i.e. –, giving[1, 9, 3]. Both keep the original input order. - Ordering: arranging all six values from smallest to largest gives
[1, 2, 3, 5, 8, 9]for thesortedkey; its ends matchminandmax. - Notice: the two portions only stay separate when , as here. For both are the whole list; for they share positions, e.g.
[10, 20, 30, 40, 50]has30in both.
Constraints
- List will have at least 3 elements
- All elements are integers