First 5 Titles from a Post List
Given a JSON array of posts (as text, simulating what response.json() would return from a posts API), print just the titles of the first 5.
Approach: json.loads() the array into a list of dicts, slice the first 5 with [:5], and print each one's title field.
Input: One line: a JSON array of post objects, each with a title field.
Output: The title of each of the first 5 posts, one per line (fewer if there are less than 5).
[{"title": "First"}, {"title": "Second"}, {"title": "Third"}, {"title": "Fourth"}, {"title": "Fifth"}, {"title": "Sixth"}]First Second Third Fourth Fifth
- Each object in the array has a title field.
Hint 1
json.loads(raw) turns the JSON array text into a real Python list of dicts.
Hint 2
posts[:5] slices out at most the first 5 items, even if the list is shorter.
Hint 3
Loop over the slice and print post["title"] for each one.
json.loads(raw) parses the JSON array into a real Python list of dicts, exactly as response.json() would from a live posts API. posts[:5] safely takes at most the first 5 entries (fewer if the list is shorter), and looping over that slice to print post["title"] gives just the titles, one per line.