def count_chars(word): # creates a bag of chars in word count_of = {} for ch in word: count_of[ch] = count_of.get(ch, 0) + 1 return count_of print count_chars("teal") def anagram(word1, word2): return count_chars(word1) == count_chars(word2) print anagram("teal", "late") print anagram("teal", "latte") # Alternate: Print the word counts. # for (word, count) in count_of.items(): # print word, ' = ', count