From 5d5a29649f3e932a0acb4e3a365b9684d1037af4 Mon Sep 17 00:00:00 2001
From: Karol Gasparik <40894232+garolpe@users.noreply.github.com>
Date: Sat, 8 Dec 2018 20:33:36 +0100
Subject: [PATCH] update: lambda expression example for python sort (#26044)
Added example that use lambda expression as key during sorting list.
---
guide/english/python/lists/list-sort-method/index.md | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/guide/english/python/lists/list-sort-method/index.md b/guide/english/python/lists/list-sort-method/index.md
index a8e6b4ce13..ef30630898 100644
--- a/guide/english/python/lists/list-sort-method/index.md
+++ b/guide/english/python/lists/list-sort-method/index.md
@@ -71,6 +71,17 @@ b.sort(key = compareByAge)
#Output
print b # prints [('Adam', 20), ('Rahul', 25), ('Rahman', 30)]
```
+You can also use Lambda expression instead of full function to define key.
Here is example of sorting strings based on last two characters:
+```py
+# Our strings can contain any characters and we want to sort them based on last two of them
+strings = ["apple_05", "orange_01", "strawberry_03", "pear_04", "banana_02"]
+
+# Take just last two characters as key value
+strings.sort(key = lambda x: x[-2:])
+
+# Output
+print strings # Prints ['orange_01', 'banana_02', 'strawberry_03', 'pear_04', 'apple_05']
+```
### Sorting Basics