Why are you calling `list()` on `course_ids` before iterating over it? Just iterate over the set; there's nothing wrong with doing that.