TransWikia.com

How to sort a 2D list of strings by length in python?

Stack Overflow Asked on November 7, 2021

I have a 2D list in python which contains first and last names of different people. It looks like this :
teachers=[["Rachelle", "Goodyear"],["Mike","Anderson"], ["Jeremy","Smith"]]
And I want to sort them by the length of their last name.
I’ve tried different solutions but it always sort by their first name … Please help me. Thank you.

One Answer

Here is a possible solution:

sorted_teachers = sorted(teachers, key=lambda x: len(x[1]))

Answered by Riccardo Bucco on November 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP