title: The most efficient way - testing 2 strings for anagrams Python
body: <p>There're many ways of <a href="https://stackoverflow.com/questions/35649771/anagram-test-for-two-strings-in-python#">testing if strings are anagrams</a>. However, I wonder if there is a way to iterate over each word only once? And if not what's the most efficient way to do it in Python?</p>
  
  <p>We can traverse through the second string checking whether each character is present in the first string. However that gives us n-1 iterations over the first string at worst case scenario (reversgram), when using build-in <a href="https://docs.python.org/3/reference/datamodel.html#object.__contains__" rel="nofollow noreferrer"> <code>__contains__()</code> </a> method (<a href="https://docs.python.org/3/reference/datamodel.html#object.__iter__" rel="nofollow noreferrer"><code>__iter__()</code></a> method is called).</p>
  
  <pre class="lang-py prettyprint-override"><code>def is_anagram(str_1, str_2):
    #chceck if same length
    if (len(str_1) != len(str_2)):
      return False
    else:
      #lowercase all characters  
      str1, str2 = list(str_1.lower()),list(str_2.lower())
      for letter in str1:
        if letter not in str2:
          return False
        str2.remove(letter)
      return True
  </code></pre>
  
  <p>Is there any other way?</p>