Or better, appending None to a list won't do anything, so sorted_units.append(unit_map.pop(item, None)) works without the exception check.