By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,979 Members | 943 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,979 IT Pros & Developers. It's quick & easy.

groupby and itemgetter

P: n/a
Hello,

there is an example how to use groupby in the itertools documentation
(http://docs.python.org/lib/itertools-example.html):

# Show a dictionary sorted and grouped by value
>>from operator import itemgetter
d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
di = sorted(d.iteritems(), key=itemgetter(1))
for k, g in groupby(di, key=itemgetter(1)):
.... print k, map(itemgetter(0), g)
....
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
3 ['g']

Now i wonder why itemgetter is used in this example. More
straightforward is:
>>d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
di = sorted(d.iterkeys(), key=d.get)
for k, g in groupby(di, key=d.get):
.... print k, list(g)
....
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
3 ['g']

This code does not need the operator module, and its also faster (tested
using timeit). Why was the, imho, more complicated version used as
example in the documentation?

Regards, Roman
Oct 6 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Roman Bertle wrote:
Hello,

there is an example how to use groupby in the itertools documentation
(http://docs.python.org/lib/itertools-example.html):

# Show a dictionary sorted and grouped by value
>>>from operator import itemgetter
d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
di = sorted(d.iteritems(), key=itemgetter(1))
for k, g in groupby(di, key=itemgetter(1)):
... print k, map(itemgetter(0), g)
...
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
3 ['g']

Now i wonder why itemgetter is used in this example. More
straightforward is:
>>>d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
di = sorted(d.iterkeys(), key=d.get)
for k, g in groupby(di, key=d.get):
... print k, list(g)
...
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
3 ['g']

This code does not need the operator module, and its also faster (tested
using timeit).
It looks like it's even faster if you drop the iterkeys() call and just
write:

di = sorted(d, key=d.get)

As to why itemgetter is used, I don't really know...

STeVe
Oct 6 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.