Nested Data Extractor
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
Nested dict with users and metadata
{'first_user': 'Alice', 'last_user': 'Bob', 'total_users': 2, 'all_names': ['Alice', 'Bob'], 'first_user_avg': 87.67}- Input:
datahas two parts.usersis an ordered list of records, here Alice (scores ) then Bob (scores ).metadatais a separate record holding a count of and a version string. - Positions:
first_useris the name in the first record,Alice.last_useris the name in the final record,Bob. With only one user, both fields would hold the same name. - Lookup:
total_usersis copied from the count stored inmetadata(here ). It is not computed from the users, so trust the stored value. - Collection:
all_namesgathers every user's name in their original order:['Alice', 'Bob']. - Average: only the first user's scores count. , rounded to 2 decimal places gives
87.67. Bob's scores never enter the result. - Notice: a single score like still averages to a float, , not an integer.
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