def navigation_items_object(self, slug): """ Flatten the items by category and then get prev and next based on sorted cat """ temp_todo_list = [] navigation_items = Bunch(prev=None, current=None, next=None) for cat, items in self.todos_by_cat.iteritems(): if items: for i in items: temp_todo_list.append(i) for c, item in enumerate(temp_todo_list): try: previous = temp_todo_list[c-1] except IndexError: previous = None try: next = temp_todo_list[c+1] except IndexError: next = None if str(item.slug) == str(slug): navigation_items.prev = previous navigation_items.current = item navigation_items.next = next # exit the forloop break return navigation_items