vanchy
vanchy

Reputation: 13

Python: How to find same value in lists and group them in a new list?

I have several lists in a list like this:

data_list = [ ['W_921', 'OSA15', 'name1', 'name2'], 
              ['W_818', 'OSA15', 'name1', 'name2'], 
              ['W_936', 'LSG2', 'name4', 'name5'], 
              ['W_936', 'OSA15', 'name1', 'name2'], 
              ['W_703', 'EB2', 'name6', 'name7'] ]

What I need is to group on the second element of the lists and create a new list like this:

grouped_data_list = [ ['OSA15', 'name1', 'name2', 'W_921, W_818, W_936'], 
                     ['LSG2', 'name4', 'name5', 'W_936'], 
                     ['EB2', 'name6', 'name7', 'W_703'] ]

Should I better work with dictionaries to achieve this? I had a look at the itertools but could not find a way to get to my desired result.

Upvotes: 1

Views: 56

Answers (1)

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

If you can use pandas, then using pandas.DataFrame.groupby, you can achieve this:

>>> df = pd.DataFrame(data_list)
>>> ( df.groupby(list(df.columns[1:]), sort=False)
        .agg(', '.join).reset_index().to_numpy().tolist() )

[['OSA15', 'name1', 'name2', 'W_921, W_818, W_936'],
 ['LSG2', 'name4', 'name5', 'W_936'],
 ['EB2', 'name6', 'name7', 'W_703']]

Upvotes: 2

Related Questions