-1

I have a list with sublist,

mylist = [['loan', 'finance'], ['dealer', 'dealers', 'dealership'], ['Data entry', 'data entry', 'enter data']]

I want to sort by using the length of the sublist, so that the output is expected as

reqlist = [['dealer', 'dealers', 'dealership'], ['Data entry', 'data entry', 'enter data'], ['loan', 'finance']]

I was trying to use sorting function

 def take(element): 
    return len(element)
 reqlist = sorted(reqlist, key = take)

I tried list.sort

 mylist.sort(key=len)

it doesnt work and output 'reqlist' is not as expected and is same as 'mylist'. Any help required.

Raady
  • 1,686
  • 5
  • 22
  • 46
  • 2
    So what exactly is your question? Did your solution not give you the results you wanted? What did it output vs what did you want it to output? – Cory Kramer Nov 27 '18 at 14:04
  • 5
    is `reqlist = sorted(data, key=take, reverse=True)` what you are looking for? Because otherwise, it seems to be working – Ma0 Nov 27 '18 at 14:04
  • edit made, please check. the output is not as expected. – Raady Nov 27 '18 at 14:07
  • 1
    as by @Ev.Kounis comment: the sorting works just fine, sorting for length 2,3,3. But you want reverse sorting. – planetmaker Nov 27 '18 at 14:09

1 Answers1

0
reqlist = sorted(mylist, key=len, reverse=True)
Vikrant Sharma
  • 419
  • 3
  • 6