📘
Nested Data Extractor
MediumPython Data Structures
Problem Statement
Extract data from nested dictionaries and lists.
Background
Real-world data often has nested structures. Use chained indexing to access nested values:
data["users"][0]["name"]
Your Task
Write a function extract_info(data) that extracts specific values from a nested structure.
The data structure is:
{
"users": [
{"name": "...", "scores": [...]},
...
],
"metadata": {"count": ..., "version": ...}
}
Output Format
Return a dictionary with:
- "first_user": Name of first user
- "last_user": Name of last user
- "total_users": Value from metadata.count
- "all_names": List of all user names
- "first_user_avg": Average of first user's scores
Example:
Input:
Nested dict with users and metadata
Output:
{'first_user': 'Alice', 'last_user': 'Bob', 'total_users': 2, 'all_names': ['Alice', 'Bob'], 'first_user_avg': 87.67}Reasoning:
Access users[0]['name'], users[-1]['name'], metadata['count'], etc.
Constraints:
- Data always has the structure shown
- At least one user exists
- Scores are non-empty lists of numbers
- Round average to 2 decimal places
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.