title: Can I change this three loops into one or do something else to make this code faster?
body: <p>I need help with this code in python 'cause this is too slow for me. I'd like to make this faster but I have no idea how. I think loops have to be less. I have even calculated that (length - 2) ^ 3 = length^3 + 12 * length - 6 * (length^2) - 8 but I don't have any concepts how to use it. Please help! Code :</p>
  <pre><code>word = &quot;a word&quot;
  letter_1 = &quot;&quot;
  letter_2 = &quot;&quot;
  letters = list(word)
  length = len(letters)
  string = &quot;&quot;
  number = 0
  number2 = 0
  words = []
  
  for i in range(length - 2):
      letter_1 = letters[i]
  
      for j in range(i + 1,length - 1):
          
          if letters[j] != letter_1:
              letter_2 = letters[j]
              
              for k in range(j + 1, length):
                  
                  if letters[k] != letter_2:
                      string = letter_1 + letter_2 + letters[k]
                      number += 1
                      
                      if not string in words:
                          words.append(string)
                          number2 += 1
  </code></pre>