Recall & Review
beginner
What is the main difference between a list and a set in Python?
A list keeps the order of items and allows duplicates, while a set does not keep order and automatically removes duplicates.
Click to reveal answer
beginner
How do you declare a list of integers in a FastAPI request body using Pydantic?
Use
List[int] from typing in the Pydantic model to specify a list of integers.Click to reveal answer
intermediate
How can you validate a set of strings in FastAPI using Pydantic?
Use
Set[str] from typing in the Pydantic model. FastAPI will ensure the input is a set with unique strings.Click to reveal answer
intermediate
Why might you choose a set over a list for input validation in FastAPI?
Because sets automatically remove duplicates, they are useful when you want to ensure all items are unique without extra code.
Click to reveal answer
intermediate
What happens if you send duplicate items in a list validated as a set in FastAPI?
FastAPI will convert the input to a set, removing duplicates before passing it to your endpoint logic.
Click to reveal answer
In FastAPI, which type hint ensures the input is a list of strings?
✗ Incorrect
List[str] tells FastAPI to expect a list of strings, preserving order and duplicates.
Which Python type automatically removes duplicate items?
✗ Incorrect
Sets do not allow duplicates and automatically remove them.
How do you import the type hint for a set in FastAPI models?
✗ Incorrect
Set is imported from the typing module for type hints.
If you want to accept a list but ensure no duplicates, what should you do in FastAPI?
✗ Incorrect
Using Set automatically removes duplicates during validation.
What will FastAPI do if the client sends duplicates but the model expects a Set?
✗ Incorrect
FastAPI converts the input to a set, removing duplicates without error.
Explain how to validate a list and a set in FastAPI using Pydantic models.
Think about how Python types affect validation and data shape.
You got /5 concepts.
Describe a real-life example where you would prefer set validation over list validation in a FastAPI app.
Consider situations where duplicates cause problems.
You got /4 concepts.