xs = [
[1,2,3,4],
[5,6,7,8],
[9,0,0,1],
[2,3],
[0],
[5,8,3,2,5,1],
[6,4],
[1,6,9,9,2,9]
]
""" expected output:
xs_dict = {
1: [[0]]
2: [[2,3],[6,4]]
4: [[1,2,3,4],[5,6,7,8],[9,0,0,1]]
6: [[5,8,3,2,5,1],[1,6,9,9,2,9]]
}
"""
I can do this, for example, by
xs_dict = {}
for x in xs:
aux = xs_dict.get(len(x),[])
aux.append(x)
xs_dict[len(x)] = aux
print(xs_dict)
But I can't help feeling there should be a more pythonic way to achieve this.
What is it?