v9.1.1
Menu

Nested Data Extractor

mediumFREELists & DictionariesPython Data StructuresPython Foundations

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
  • Input: data has two parts. users is an ordered list of records, here Alice (scores 90,85,8890, 85, 88) then Bob (scores 78,82,8078, 82, 80). metadata is a separate record holding a count of 22 and a version string.
  • Positions: first_user is the name in the first record, Alice. last_user is the name in the final record, Bob. With only one user, both fields would hold the same name.
  • Lookup: total_users is copied from the count stored in metadata (here 22). It is not computed from the users, so trust the stored value.
  • Collection: all_names gathers every user's name in their original order: ['Alice', 'Bob'].
  • Average: only the first user's scores count. 90+85+883=2633≈87.6667\frac{90 + 85 + 88}{3} = \frac{263}{3} \approx 87.6667, rounded to 2 decimal places gives 87.67. Bob's scores never enter the result.
  • Notice: a single score like [50][50] still averages to a float, 50.050.0, 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

Learn this in

More from Python Foundations

solution.py

Test results

Sign in to run your code against the test cases.