How to Sort a List Rows according to its Sublist in Python


In this example we will show how to sort list rows based on the second element in the sublist with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

a=[["a", 25],["C", 32],["G", 12],["g", 18]]
a.sort(key=lambda x:(x[1],x[0]))
print(a)

Output:

[['G', 12], ['g', 18], ['a', 25], ['C', 32]]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments