Skip to content
Advertisement

Operator Itemgetter not giving proper group count in the dictionary?

  • The dictionary is below
  • I need to extract the count for id and empid basically group count
JavaScript

Code is below

JavaScript

My output is not giving proper output

{'id': 'ANNW', 'empid': 'YUZP336'} the above count is actually it’s 12 but I am getting 3 times its repeating like

JavaScript

Like above i need to extract all the id and empid

JavaScript

Advertisement

Answer

For itertools.groupby to work as intended, you need to sort the data first. So try passing sorted(res, key=grouper) instead of res in groupby:

JavaScript

which gives

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement