0

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")

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57

2 Answers2

2

You could unpack the sublists and zip:

out = list(map(list, zip(*l)))

Output:

[['user1/stats1.csv', 'user2/stats1.csv'],
 ['user1/stats2.csv', 'user2/stats2.csv'],
 ['user1/stats3.csv', 'user2/stats3.csv']]
0

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']]