Suppose you wanted to sort a collection of strings by the number of distinct letters in each string:
strings = ['foo', 'card', 'bar', 'aaaa', 'abab']
Here we could pass a lambda function to the list’s sort method:
strings.sort(key=lambda x: len(set(list(x))))
strings
['aaaa', 'foo', 'abab', 'bar', 'card']