On Sun, Jan 11, 2009 at 2:02 PM, Alexandre Fiori <fio...@gmail.com> wrote: > > hello > > i was thinking about a possible improvement for the itemgetter > the documentation page shows simple examples like sorting a dictionary by > its integer values
Hi, Sorry for starting like this but ideas are supposed to be emailed to the python-ideas maillist. > . > . > > in order for that sort (and possibly a lot of other things) to work > properly, we could add > a callback method for itemgetter, like this: > > class itemgetter: > def __init__(self, index, callback=None): > self.index = index > self.callback = callback > > def __call__(self, item): > return self.callback and self.callback(item[self.index]) or > item[self.index] > > so, we could easly sort by the amount of data in each list, like this: > >>>> sorted(friends.items(), key=itemgetter(1, callback=len)) > [('john', ['max']), ('alex', ['bob', 'jane']), ('foo', ['bar', 'steve', > 'linda'])] > > > what do you guys think about it? please correct me if i'm wrong. > > You are not forced to use itemgetter as a key in sorted, you can provide your own key method, like this: def x(item): return len(item[1]) sorted(friends.items(), key=x) Also, your idea ruins the name "itemgetter" since it is no longer a itemgetter. -- -- Guilherme H. Polo Goncalves _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com