'Pythonic way to group a list of lists? [duplicate]

So lets say I have a hypothetical list of lists of file names in Python defined like so:

l = [["user1/stats1.csv", "user1/stats2.csv", "user1/stats3.csv"], 
    ["user2/stats1.csv", "user2/stats2.csv", "user2/stats3.csv"]]

What would be the most pythonic way to group it by the number in statsN.csv such that the list would look like:

l = [["user1/stats1.csv", "user2/stats1.csv"], 
    ["user1/stats2.csv", "user2/stats2.csv"],
    ["user1/stats3.csv", "user2/stats3.csv"],

For reference, the original list was obtained by using glob with the * wild card a la glob.glob("user1/stats*.csv") and glob.glob("user2/stats*.csv")



Solution 1:[1]

If you're using numpy, you can simply transpose:

>>> np.array(l).T.tolist()
[['user1/stats1.csv', 'user2/stats1.csv'],
 ['user1/stats2.csv', 'user2/stats2.csv'],
 ['user1/stats3.csv', 'user2/stats3.csv']]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1