From the course: Python Data Analysis (2020)

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Anagrams overview

Anagrams overview

- [Instructor] In chapter two, we have reviewed Python loops, data containers, and comprehensions. Now we will set them to work in a simple, practical project, finding anagrams in the English dictionary. As you know, two words are anagrams of each other when their letters can be rearranged to turn one word into the other. For instance, elvis and lives. We will use this simple strategy to find anagrams. We defined a signature of a word as the sorted list of its letters, including duplicates. So the signature of post would be opst. And then we find that spot, stop, tops, pots, and opts have the same signature as post and therefore they're all anagrams of each other. We're going to make a Python dict of all the words in the dictionary indexed by signature. And then to find out if a word has an anagram, we will just compute the signature and look it up in the dict. Let's begin.

Contents