tail

Python Morsels Exercise

Return last N items in a given iterable

2 bonuses 5 hints 11 solutions 1,148 users solved 73 reviews

I want you to make a function that takes a sequence (like a list, string, or tuple) and a number n and returns the last n elements from the given sequence, as a list.

For example:

>>> tail([1, 2, 3, 4, 5], 3)
[3, 4, 5]
>>> tail('hello', 2)
['l', 'o']
>>> tail('hello', 0)
[]

Bonus 1


This is just a preview of the problem statement.

This exercise includes 2 bonuses, 5 hint links, and automated tests.

To solve this exercise, sign in to your Python Morsels account.

A learning habit for experienced Pythonistas

Profile picture of Trey

My name is Trey Hunner and I hold Python trainings for teams. I've spent countless hours refining my most interesting Python exercises into Python Morsels, a Python skill-building platform for folks who already know Python.

Python Morsels is design to help you improve your Python skills every week through 1 hour of deliberate practice.

Let me help you write more beautiful Python code. Sign up for Python Morsels to access this exercise and many more.

Join Python Morsels ✨