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