0

I want to sort dictionary keys by values using sorted function with lambda function

word_count = {"a":25, "and":10, "the":15,"for":9, "or":7}

using this sorted function:

x = sorted(word_count.items(), key=lambda(k,v):v, reverse=True)

This function works in Python 2.7, but in Python 3 I'm getting Syntax error.

Could you pls someone explain why this sorting function doesnt work in Python3? Thx.

madmax80
  • 171
  • 1
  • 14
  • `sorted(word_count.items(), key=lambda x: x[1], reverse=True)` – Chris_Rands Mar 15 '18 at 15:26
  • thx, but this is different syntax, i wanted explanation why my solution doesnt work in Python3 but works in Python2 – madmax80 Mar 15 '18 at 15:41
  • Ok, I found that tuple paramater unpacking was removed in Python3, that was the reason why my solution didnt work in Python3 https://www.python.org/dev/peps/pep-3113/ – madmax80 Mar 15 '18 at 16:13

0 Answers0