categorizing string according to length in Python
Question: I have given n different strings. I have to form different lists of strings
according to the list. For example, given strings are
`this,that,is,am,are,i,j` then four lists should be generated like:

    
    
    s[1] = [i,j]
    s[2] = [is,am]
    s[3] = [are]
    s[4] = [this,that]
    

These lists must be easily accessible by their index (in this case, length)


Answer: You could use
[defaultdict](https://docs.python.org/2/library/collections.html#collections.defaultdict):

    
    
    from collections import defaultdict
    
    words = ['this','that','is','am','are','i','j']
    
    with_lengths = [(word, len(word)) for word in words]
    output = defaultdict(list)
    for k, v in with_lengths:
      output[v].append(k)
    
    for key in output:
      print '%i -> %s' % (key, output[key])
    

Output:

    
    
    1 -> ['i', 'j']
    2 -> ['is', 'am']
    3 -> ['are']
    4 -> ['this', 'that']
    

