Sort using Lambda in Python

How to perform a sort with lambda in python? A lambda function is a small anonymous function, it can be created or defined using the lambda keyword. You can use lambda as the key function when sorting a list. for example, use key=lambda to the sort() function to order list.

Python lambda is used to execute an anonymous function. This function can take any number of arguments, but can only have one expression and they can be used wherever function objects are required.

1. Syntax

Following is the syntax of the python sort() and sorted() functions using lambda as key.


# syntax of sort()
list.sort(key=lambda, reverse)

# syntax of sorted()
sorted(iterable,key=lambda, reverse)

2. Python list.sort() with Lambda

By using the list.sort() function you can order a list of numbers in ascending or descending order and use key=lambda to specify the sort function as python lambda, it also takes reverse param which is a boolean value that specifies whether the list should be sorted in ascending (False) or descending (True) order. The default is reverse=False.

The key function is an optional parameter that can be used to specify how the items should be sorted. When you use key=lambda, The lambda function is applied to each item in the list, and the resulting values are used to determine the sort order.


# Create list with numbers
numbers = [2, 4, 1, 6, 3]
print("Original:",numbers)

# Using sort() with lambda
numbers.sort(key=lambda x:x)
print("Sorted:",numbers)

Yields below output.


# Output:
Original: [2, 4, 1, 6, 3]
Sorted: [1, 2, 3, 4, 6]

3. Python sorted() with Lambda

Alternatively, the sorted() function in Python is used to sort a sequence (such as a list, or tuple) of numbers in ascending order. The function returns a new sorted list, leaving the original sequence unchanged. Similar to the above method, use the key=lambda to specify the sort function.


# Create list with numbers
numbers = [2, 4, 1, 6, 3]
print("Original:",numbers)

# Using sorted() with lambda
sorted_numbers = sorted(numbers, key=lambda x: x)
print("Sorted:",sorted_numbers)

Yields below output.


# Output:
Original: [2, 4, 1, 6, 3]
Sorted: [1, 2, 3, 4, 6]

4. Sort Reverse Order using Lambda

Use reverse=True and key=lambda as params to the list.sort() or sorted() functions to order the elements in descending or reverse order. I have a dedicated article on sorting in reverse order in Python.


# Create list with numbers
numbers = [2, 4, 1, 6, 3]
print("Original:",numbers)

# Using sort() with lambda
numbers.sort(key=lambda x:x, reverse=True)
print("Revers order using sort:",numbers)

# Using sorted() with lambda
sorted_numbers = sorted(numbers, key=lambda x: x, reverse=True)
print("Revers order using sorted:",sorted_numbers)

Yields below output.


# Output:
Original: [2, 4, 1, 6, 3]
Revers order using sort: [6, 4, 3, 2, 1]
Revers order using sorted: [6, 4, 3, 2, 1]

5. Sorting List of Tuples using Lambda

Finally, let’s sort the list of tuples using the lambda function. Here, you need to select the index on which you wanted to select. x[1] means sort on second position.


# Sort list of tuples
lst = [('Ann','20','400'), ('Scott','40','500'), ('Bean','10','450')]
lst.sort(key=lambda x:x[1])
print(lst)

Yields below output.


# Output:
# [('Bean', '10', '450'), ('Ann', '20', '400'), ('Scott', '40', '500')]

Conclusion

In this article, you have learned how to sort the list elements by using the python lambda function. For example you can use the lambda as the key function when sorting a list ( key=lambda). Use this either with the sort() or sorted() function to order iterable like list, tuple using lambda.

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply