parse_ranges

Python Morsels Exercise

Parse comma-delimited numeric ranges

3 bonuses 7 hints 23 solutions 1,075 users solved 51 reviews

I'd like you to write a function called parse_ranges, which accepts a string containing ranges of numbers and returns an iterable of those numbers.

The numeric ranges in the string will consist of two numbers separated by hyphens and each of the ranges will be separated by commas and any number of spaces.

Your function should work like this:

>>> parse_ranges('1-2,4-4,8-13')
[1, 2, 4, 8, 9, 10, 11, 12, 13]
>>> parse_ranges('0-0, 4-8, 20-20, 43-45')
[0, 4, 5, 6, 7, 8, 20, 43, 44, 45]

In the examples above the functions return lists of numbers. Your function can return any iterable of numbers that you'd like though.

If you finish the main problem, try to solve at least one of the bonuses.

Bonus 1


This is just a preview of the problem statement.

This exercise includes 3 bonuses, 7 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 ✨