-3

How do I sort the below list of tuples by the second element?

[('8B', u'11'), ('8P', u'2.3'), ('8C', u'1.6')]

Expected Result would be:

[('8C', u'1.6'), ('8P', u'2.3'), ('8B', u'11')]
Idos
  • 14,552
  • 14
  • 54
  • 70
TidyWay
  • 84
  • 1
  • 1
  • 9

1 Answers1

2

Tested and confirmed in Python IDLE

my_list = [('8B', u'11'), ('8P', u'2.3'), ('8C', u'1.6')]
my_list.sort(key=lambda x: float(x[1]))
print(my_list)

Got

[('8C', '1.6'), ('8P', '2.3'), ('8B', '11')]
coralvanda
  • 5,561
  • 2
  • 12
  • 21