2

I have a list which looks like this,

[('1', 'Michael'),
('2', 'Christopher'),
('3', 'Matthew'),
('4', 'Joshua'),
('5', 'Daniel'),
('6', 'David'),
('7', 'Andrew'),
('8', 'James'),
('9', 'Justin'),
('10', 'Joseph'),
('11', 'Ryan'),
('12', 'John'),
('13', 'Robert'),
('14', 'Nicholas'),]

Im trying to sort it using the second element,which is the names,im not exactly sure how to do this, This is what i have tried,but i cant get it to work,

sorted(mod , key = lambda x:mod[1])
sorted(mod, key = [x for x,v in enumerate(mod) mod[x][1]]

Any suggestions will be helpful,thanks in advance.

Ryan
  • 8,459
  • 14
  • 40
  • 66
  • https://stackoverflow.com/questions/10695139/sort-a-list-of-tuples-by-2nd-item-integer-value – Shiva Dec 21 '17 at 07:51

2 Answers2

1
l= [('1', 'Michael'),
('2', 'Christopher'),
('3', 'Matthew'),
('4', 'Joshua'),
('5', 'Daniel'),
('6', 'David'),
('7', 'Andrew'),
('8', 'James'),
('9', 'Justin'),
('10', 'Joseph'),
('11', 'Ryan'),
('12', 'John'),
('13', 'Robert'),
('14', 'Nicholas'),]

result = sorted(l,key=lambda t:t[1])
print(result)
Fuji Komalan
  • 1,979
  • 16
  • 25
1

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

Will
  • 792
  • 1
  • 5
  • 22