my_list = ['apple', 'pear', 'orange', 'raspberry']
# I know that I'm always looking for pear.
print 'pear' in my_list # prints True
# I want to be able to get a key by its value.
pear_key = my_list['pear'].key # should be 1
# Print the next item in the list.
print my_list[pear_key + 1] # should print orange
I know that pear will always be an item in my list (not the position though), and I’m looking for a way to get the value of the next item in that list, either by getting the current key by knowing its value and advancing it by one (like I did in the example above) or by using something like my_list.next.
You could of course pre-cache it, by using (think it’s probably the itertools pair recipe)
But then you lose the fact of whether it was in the original list, or just didn’t have a logical next value.