title: Python: How to group a list of objects by their characteristics or attributes?
body: <p>I want to separate a list of objects into sublists, where objects with same attribute/characteristic stay in the same sublist.</p>
  
  <p>Suppose we have a list of strings:</p>
  
  <pre><code>["This", "is", "a", "sentence", "of", "seven", "words"]
  </code></pre>
  
  <p>We want to separate the strings based on their length as follows:</p>
  
  <pre><code>[['sentence'], ['a'], ['is', 'of'], ['This'], ['seven', 'words']]
  </code></pre>
  
  <p>The program I currently come up with is this</p>
  
  <pre><code>sentence = ["This", "is", "a", "sentence", "of", "seven", "words"]
  word_len_dict = {}
  for word in sentence:
      if len(word) not in word_len_dict.keys():
          word_len_dict[len(word)] = [word]
      else:
          word_len_dict[len(word)].append(word)
  
  
  print word_len_dict.values()
  </code></pre>
  
  <p>I want to know if there is a better way to achieve this?</p>