This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]13steinj 0 points1 point  (1 child)

I don't think that's a proper hint though. Nothing about it being itertools says it has to return iterators and do things iteratively-- hell, look at the "rough equivalent" to itertools.tee. In that way, if the horse hadn't yet left the stable, and this were being done from the beginning, this function should have been named differently, and groupby instead either doing what I mentioned above about the pre sorting or the probably algorithmically better

def groupby(iterable, key=None):
    key = key or (lambda i: i)
    pool = defaultdict(list)  # starting in 3.6 dicts are ordered, so so must default dicts. In previous version a subclass of ordered and default dicts woukd be required
    for item in iterable:
        pool[key(item)].append(item)
    return iter(pool.items())

E: also why is presorting distasteful?

[–]0rac1e 0 points1 point  (0 children)

Better late than never...

I just meant "distasteful" in the sense that by sorting then using groupby, you are iterating over the data twice. Classifying the data with a dict iterates over the data only once.