input: [\'abc\', \'cab\', \'cafe\', \'face\', \'goo\']
output: [[\'abc\', \'cab\'], [\'cafe\', \'face\'], [\'goo\']]
The problem is simple: it grou
from itertools import groupby
words = ['oog', 'abc', 'cab', 'cafe', 'face', 'goo', 'foo']
print [list(g) for k, g in groupby(sorted(words, key=sorted), sorted)]
Result:
[['abc', 'cab'], ['cafe', 'face'], ['foo'], ['oog', 'goo']]
You can't just use the groupby function, as that only groups together sequential elements for which your key function produces the same result.
The easy solution is just to sort the words first using the same function as you use for grouping.