Skip to content

Python list sort lambda expression | Example code

  • by

Use the list sort() method to sort with a lambda expression in Python. Both list.sort() and sorted() have a key parameter to specify a function to be called on each list element prior to making comparisons.

Example of how to sort with lambda in Python

Simple example code Call list sort with a key set to a lambda expression to sort the elements of a list by the key.

fruits = [("Apples", 5, "20"), ("Pears", 1, "5"), ("Oranges", 6, "10")]

fruits.sort(key=lambda x: x[0])



Python list sort lambda expression

Do comment if you have any doubts or suggestions on this Python list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.