Update classroom and returns a list of errored students

def update_classroom(self, course, classroomid, new_data):
        """ Update classroom and returns a list of errored students"""
        student_list, tutor_list, other_students, _ = self.get_user_lists(course, classroomid)

        # Check tutors
        new_data["tutors"] = [tutor for tutor in map(str.strip, new_data["tutors"]) if tutor in tutor_list]

        students, groups, errored_students = [], [], []

        new_data["students"] = map(str.strip, new_data["students"])

        # Check the students
        for student in new_data["students"]:
            if student in student_list:
                students.append(student)
            else:
                if student in other_students:
                    # Remove user from the other classroom
                    self.database.classrooms.find_one_and_update({"courseid": course.get_id(), "groups.students": student},
                                                                 {"$pull": {"groups.$.students": student, "students": student}})
                    self.database.classrooms.find_one_and_update({"courseid": course.get_id(), "students": student}, {"$pull": {"students": student}})
                    students.append(student)
                else:
                    # Check if user can be registered
                    user_info = self.user_manager.get_user_info(student)
                    if user_info is None or student in tutor_list:
                        errored_students.append(student)
                    else:
                        students.append(student)

        removed_students = [student for student in student_list if student not in new_data["students"]]
        self.database.classrooms.find_one_and_update({"courseid": course.get_id(), "default": True},
                                                     {"$push": {"students": {"$each": removed_students}}})

        new_data["students"] = students

        # Check the groups
        for group in new_data["groups"]:
            group["students"] = [student for student in map(str.strip, group["students"]) if student in new_data["students"]]
            if len(group["students"]) <= group["size"]:
                groups.append(group)

        new_data["groups"] = groups

        classroom = self.database.classrooms.find_one_and_update(
            {"_id": ObjectId(classroomid)},
            {"$set": {"description": new_data["description"],
                      "students": students, "tutors": new_data["tutors"],
                      "groups": groups}}, return_document=ReturnDocument.AFTER)

        return classroom, errored_students