0

i have a nested list that needs to be sorted alphabetic on index 1

example:

some_list = [
    [85, u'Bounty Prizes'],
    [34, u'Agent Mission Time Bonus Reward'],
    [46, u'Brokers Fee'],
    [83, u'Contract Reward Deposited'],
    [17, u'Bounty Prize'],
    [1, u'Player Trading'],
    [15, u'Repair Bill'],
    [10, u'Player Donation']
]

sorted(some_list)

but this sorts on index 0. how can I sort on the second item?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Hans de Jong
  • 2,030
  • 6
  • 35
  • 55

1 Answers1

5

Use the key argument to the sorted function

sorted(some_list, key = lambda x:x[1])

Refer: The documentation for sorted

key specifies a function of one argument that is used to extract a comparison key from each list element

Another way

>>> import operator
>>> sorted(some_list, key = operator.itemgetter(1))
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140