-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
  • 15,053
  • 14
  • 60
  • 75
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
  • 6,431
  • 2
  • 15
  • 25