The Latest Messages

Python FundamentalsMedium+10 XP+10

The Latest Messages

A chat app keeps messages in a list, oldest first. The preview shows the newest n messages, still oldest-to-newest.

Task: write latest_messages(inbox, n) that returns the last n messages of inbox, in their original order.

  • n is between 0 and the number of messages. If n is 0, return an empty list.

Examples

inbox = ["hi", "lunch?", "sure", "12:30", "ok"]
n = 2
returns ['12:30', 'ok']
inbox = ["hi", "lunch?", "sure", "12:30", "ok"]
n = 4
returns ['lunch?', 'sure', '12:30', 'ok']

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run