Need help with __iter__ and __next__ dunder methods

Hello everyone,

I am currently working on the New Teacher in Town project for Learn Intermediate Python 3.
https://www.codecademy.com/courses/learn-intermediate-python-3/projects/new-teacher-in-town-project

I have a question for step 3, How would I print my next and iter methods to the console to see if I am getting the desired output?

Here is my current code for script.py:

from roster import student_roster

class ClassroomOrganizer:
  
  def __init__(self):
    self.sorted = self.alphabetize(student_roster)

  def alphabetize(self, students):
    names = []
    for student in  students:
      for key, value in student.items():
        if key == 'name':
          names.append(value)
    return sorted(names)

 
  def __iter__(self):
    self.num = 0
    return self


  def __next__(self):
    return self.sorted[self.num]
    self.num += 1

Thank you in advance!

Hi there,

I hope I can help you with this. I do not have access to the reference source. I assume student_roster is a list of dictionaries, correct?

There is an error with the __next__ method.

def __next__(self):
    return self.sorted[self.num]
    self.num += 1

Note: Remember, code is reachable after the final Return statement within a function. So that last line is not doing anything. I made some minor changes to your code to help answer your question.

class ClassroomOrganizer:

    def __init__(self, student_roster):
        self.student_roster = student_roster
        self.sorted_roster = self.alphabetize()

    def alphabetize(self):
        names = []
        for student in self.student_roster:
            for key, value in student.items():
                if key == 'name':
                    names.append(value)
        return sorted(names)

    def __iter__(self):
        self.index = 0
        return self

    def __next__(self):
        if self.index < len(self.sorted_roster):
            t= self.sorted_roster[self.index]
            self.index += 1
            return t
        raise StopIteration


if __name__ == '__main__':
    student_roster = [{'name': 'jon', 'grade': 90}, 
                      {'name': 'amy', 'grade': 85}, 
                      {'name': 'seraph', 'grade': 100},
                      {'name': 'zed', 'grade': 55}]

    my_roster = ClassroomOrganizer(student_roster)

Question: How would I print my next and iter methods to the console to see if I am getting the desired output?

Answer: You create an iter instance of the sorted student roster list like so:


    # An iter instance:
    my_iterator = iter(my_roster.sorted_roster)  # ['amy', 'jon', 'seraph', 'zed']

    # Then use the next() method on your iter instance like so:
    print(next(my_iterator))  # amy
    print(next(my_iterator))  # jon
    print(next(my_iterator))  # seraph
    print(next(my_iterator))  # zed 
    print(next(my_iterator))  # Iteration Error

I hope this helps, if you have any further questions please feel free to ask.

Best regards,

2 Likes

Thank you so much for your help, your answer has cleared up my confusion.
Have a nice day!