0

I'm having a problem in my code where I'm trying to check if (in my case) there is a review already created with that title from that reviewer.

For that I'm doing:

def review_result(self):
    print("Complete your review")
    title = input("Title of the paper: ")
    reviewer = input("Reviewer's name: ")
    for x in self.__review:
        if x == title:
            index = self.__review.index(x)
            if self.__review[index + 1] == reviewer:

But in my self.__review list I can have the same title repeated multiple times but all with diferent reviewers, for example: ['Book1', 'Rev1', 'Book1', 'Rev2', 'Book1' Rev3']

When I have 2 reviews from the same paper I can't access the 2nd review because that for x in self.__review is only searching for the 1st value that apears.

Is there any way I can see the next 'x' in that for x in self.__review loop?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
simao-af
  • 9
  • 3
  • 1
    A different data structure would suit you better. – TomServo Nov 27 '22 at 01:57
  • 4
    `for x in self.__review` is iterating over the entire list. The problem is, as @TomServo pointed out, you've chosen the wrong data structure. Not knowing what your ultimate goal is, I would suggest trying a dict where the keys are the books and the values are a list of reviews. For your data, it would look like `{"Book1": ["Rev1", "Rev2", "Rev3"]}` – MattDMo Nov 27 '22 at 02:04
  • Do you have identical reviews? If not, you could go the other way `{"rev1":"book1", "rev2":"book1"}` – OneMadGypsy Nov 27 '22 at 02:08
  • Does this answer your question? [Iterating over every two elements in a list](https://stackoverflow.com/questions/5389507/iterating-over-every-two-elements-in-a-list) – mkrieger1 Nov 27 '22 at 02:11
  • Thank you all, I'll try to do a dict as you told, that might work for my problem :) – simao-af Nov 27 '22 at 02:19

1 Answers1

0

You can use enumerate:

for index, x in enumerate(self.__review):
    current_review = x
    next_review = self.__review[index + 1] # note that this will error when you reach the last item on the list, make sure you have handling for it!
    # more code
TNTzx
  • 407
  • 2
  • 11