user2208885
user2208885

Reputation: 31

Python: New list grouping repeated elements from existing list

I have one list of the form:

>>> my_list = ['BLA1', 'BLA2', 'BLA3', 'ELE1', 'ELE2', 'ELE3', 'PRI1', 'PRI2', 'NEA1', 'NEA2', 'MAU1', 'MAU2', 'MAU3']

and I want to create a new list, grouping the repeated elements into lists inside my new list, so at the end I will have:

>>> new_list = [['BLA1', 'BLA2', 'BLA3'], ['ELE1', 'ELE2', 'ELE3'], ['PRI1', 'PRI2'], ['NEA1', 'NEA2'], ['MAU1', 'MAU2', 'MAU3']]

Upvotes: 2

Views: 272

Answers (3)

Martijn Pieters
Martijn Pieters

Reputation: 1121266

Use itertools.groupby:

import itertools

[list(group) for key, group in itertools.groupby(my_list, key=lambda v: v[:3])]

The key argument is needed here to extract just the part of the value you wanted to group on; the first 3 characters.

Result:

>>> my_list = ['BLA1', 'BLA2', 'BLA3', 'ELE1', 'ELE2', 'ELE3', 'PRI1', 'PRI2', 'NEA1', 'NEA2', 'MAU1', 'MAU2', 'MAU3']
>>> [list(group) for key, group in itertools.groupby(my_list, key=lambda v: v[:3])]
[['BLA1', 'BLA2', 'BLA3'], ['ELE1', 'ELE2', 'ELE3'], ['PRI1', 'PRI2'], ['NEA1', 'NEA2'], ['MAU1', 'MAU2', 'MAU3']]

groupby will combine successive keys that are equal into 1 group. If you have disjoint groups (so same value, but with other values in between) it'll create separate groups for those:

>>> my_list = ['a1', 'a2', 'b1', 'b2', 'a3', 'a4']
>>> [list(group) for key, group in itertools.groupby(my_list)]
[['a1', 'a2'], ['b1', 'b2'], ['a3', 'a4']]

If that is not what you want you will have to sort my_list first.

Upvotes: 6

Dave Kirby
Dave Kirby

Reputation: 26552

As an alternative to groupby, you could use collections.Counter:

In [40]: from collections import Counter

In [41]: [ [k]*v for (k,v) in Counter(my_list).iteritems() ]
Out[41]: 
[['PRI', 'PRI'],
 ['NEA', 'NEA'],
 ['BLA', 'BLA', 'BLA'],
 ['MAU', 'MAU', 'MAU'],
 ['ELE', 'ELE', 'ELE']]

This will work without the need to sort the list if the elements are all jumbled up, unlike groupby.

Upvotes: 1

YXD
YXD

Reputation: 32511

Make sure it's sorted and use

itertools.groupy

Upvotes: 1

Related Questions