1

I have a couple of list in a list sorted like this:

L = [['b','4'].['a','7'],['c','2']]

I want to see if it is possible to arrange the list so that it looks bit like this:

L = [['a','7'],['b','4'],['c','2']]

The first character of each list is placed alphabetically. How might I go about doing this?

Edric
  • 24,639
  • 13
  • 81
  • 91

2 Answers2

3

You can use .sort() that will sort the list alphabetically

L = [['b','4'],['a','7'],['c','2']]
L.sort()

output: [['a', '7'], ['b', '4'], ['c', '2']]
David sherriff
  • 466
  • 1
  • 3
  • 9
2

use method sorted():

L = [['b','4'], ['a', '7'],['c','2']]
print(sorted(L))  # output [['a', '7'], ['b', '4'], ['c', '2']]
Tasnuva Leeya
  • 2,515
  • 1
  • 13
  • 21