Build a TypedDict for API Response
Beginner · Write the code · from NamedTuple vs TypedDict vs Dataclass: Which to Choose?
From the lesson: NamedTuple vs TypedDict vs Dataclass: Which to Choose?
Python gives you three popular ways to bundle related data together: NamedTuple, TypedDict, and dataclass. Each one solves a slightly different problem.
Read the lessonCreate a TypedDict called ApiResponse with three fields: status (str), code (int), and data (str). Create an instance with status 'success', code 200, and data 'Hello'. Print the value of the 'status' key.
from typing import TypedDict
# Create your ApiResponse TypedDict here
# Create an instance and print the status
More NamedTuple and TypedDict exercises
- Create a NamedTuple for RGB ColorsBeginner
- Predict the Output: ImmutabilityIntermediate
- Convert a Dict to a NamedTupleIntermediate
- Choose the Right ContainerAdvanced
All Python exercises · 50 practice problems · Object-Oriented Programming quiz