Skip to content
dreamcode
dreamcode
Map
Comprehension Sorter
Section challenge · IntermediatePython
Reward: +50 XP
PROBLEM

Comprehension Sorter

Write a function filter_stars(stars, min_mag) that takes a list of dictionaries representing stars, e.g. [{"name": "Sirius", "mag": -1.46}], and a float min_mag. It should return a dictionary mapping star names to their magnitudes, but only for stars with a magnitude strictly greater than min_mag (lower brightness). Use a dictionary comprehension.

Examples
filter_stars([{'name': 'Sirius', 'mag': -1.46}, {'name': 'Vega', 'mag': 0.03}], -1)
{'Vega': 0.03}
filter_stars([{'name': 'Sirius', 'mag': -1.46}], 0)
{}
solution.py
PYTHON
Saved as you type

Tests

0 of 4 passing
  • filter brightness
    filter_stars([{'name': 'Sirius', 'mag': -1.46}, {'name': 'Vega', 'mag': 0.03}], -1)
    expected {'Vega': 0.03}
  • filter all
    filter_stars([{'name': 'Sirius', 'mag': -1.46}], 0)
    expected {}
  • empty list
    filter_stars([], 0)
    expected {}
  • all dimmer
    filter_stars([{'name': 'Deneb', 'mag': 1.25}, {'name': 'Polaris', 'mag': 1.97}], 1)
    expected {'Deneb': 1.25, 'Polaris': 1.97}
On the line
+50 XP
Pass all 4 tests to claim it.