# Example usage: your_list = ['cmd1','cmd10', 'cmd111', 'cmd50', 'cmd99'] your_list.sort(key=lambda x: int(x[3:])) print(your_list) --> ['cmd1', 'cmd10', 'cmd50', 'cmd99', 'cmd111']
Here is what the above code is Doing:
1. We have a list of strings, each of which starts with ‘cmd’ and is followed by a number.
2. We want to sort the list by the number, so we use the sort() method.
3. We use the key parameter to specify a function that will be used to sort the list.
4. The function we specify is a lambda function that takes a string as input and returns the integer after the ‘cmd’ part of the string.
5. This function is used to sort the list, so the list is sorted by the numbers after the ‘cmd’ part of the strings.