How to split a list of different anagram words into separate lists in pure python?

I have this list of anagram words I’d like to split into nested lists if they have the same letters.

mylist = ["pots", "stop", "levi",  "vile", "evil", "spot", "star", "rats", "bingo", "live", "tops" "gobin"]

Result:

anagrams = [["pots", "stop", "spot", "tops"],
["levi", "live", "vile", "evil"],
["star", "rats"],
["bingo", "gobin"]]

I’d like to split them using pure python, i.e. not using any python packages such as groupby from itertools

Any help is very much appreciated.

Answer

You can use a dict with key as sorted word

mylist = ["pots", "stop", "levi",  "vile", "evil", "spot", "star", "rats", "bingo", "live", "tops", "gobin"]

res = {}

for i in mylist:
    res.setdefault(tuple(sorted(i)), []).append(i)

print(list(res.values()))

Output

[['pots', 'stop', 'spot', 'tops'], ['levi', 'vile', 'evil', 'live'], ['star', 'rats'], ['bingo', 'gobin']]