I want to get all the maximum values from an iterator:
def max_val(iterator, key=None):
# ???
it = (i for i in range(4))
assert max_val(it, key=lambda i: i%2) == [1, 3]
Note: this question is similar to what was asked before for a list
.
There are 2 differences with the previous question:
1) I want this to work for an iterator, so I can't use the 2-pass approach that was both the fastest and the simplest solution to the list question.
2) I want to get the maximum values rather than indices. It might sound weird, but this is useful if I specify a key argument as in the above example (or if the objects have ordering based only on part of their internal state).
One solution is a slightly modified @martineau answer to the list question (it would need to store the values instead of indices), but I was wondering if there's a faster approach.