Python Fundamentals Practice Problems - 13. Anagrams - Alternative solution in 4 lines

Screen Link:

def are_anagrams(string1, string2):
    if sorted(string1) == sorted(string2):
        return True
        return False

The built-in method sorted(a_string) returns a list of the characters of a_string sorted alphabetically. To determine if 2 strings are anagrams, we can simply sort them both in separate lists, then check whether the lists are identical.

This is an easy alternative but it’s not as efficient since creating 2 lists and comparing them costs more memory and time. I wanted to share it nonetheless, because I find this built-in method very versatile (I bet I’ll encounter it in future courses / missions).