-1

How would i sort this list, based on the integer inside the 2nd list?

import random

l= []

for i in range(10):
    l.append(['xyz', random.randint(1,100)])


print(l)

Thank you!

  • (There's no difference here between sorting a list of tuples and a list of lists, and so the answers to the linked question work without modification.) – DSM Sep 15 '14 at 20:06

1 Answers1

1
In [13]: l.sort(key=operator.itemgetter(1))

In [14]: l
Out[14]: 
[['xyz', 6],
 ['xyz', 7],
 ['xyz', 17],
 ['xyz', 41],
 ['xyz', 52],
 ['xyz', 54],
 ['xyz', 62],
 ['xyz', 66],
 ['xyz', 68],
 ['xyz', 69]]
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241