Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting list into smaller lists of equal values

Tags:

python

I am looking to transform a list into smaller lists of equal values. An example I have is:

["a", "a", "a", "b", "b", "c", "c", "c", "c"] 

to

[["a", "a", "a"], ["b", "b"], ["c", "c", "c", "c"]]

What do you think is the most efficient way to do this?

like image 462
Enesxg Avatar asked Nov 21 '25 09:11

Enesxg


2 Answers

You could use itertools.groupby to solve the problem:

>>> from itertools import groupby
>>> [list(grp) for k, grp in groupby(["a", "a", "a", "b", "b", "c", "c", "c", "c"])]
[['a', 'a', 'a'], ['b', 'b'], ['c', 'c', 'c', 'c']]

It only groups consecutive equal elements but that seems enough in your case.

like image 180
MSeifert Avatar answered Nov 23 '25 23:11

MSeifert


You could use collections.Counter

>>> lst = ["a", "a", "a", "b", "b", "c", "c", "c", "c"]
>>> import collections
>>> collections.Counter(lst).most_common()
[('c', 4), ('a', 3), ('b', 2)]

This works even when the values are not ordered and provides a very compact representation which then you could expand if needed into lists:

>>> [[i]*n for i,n in collections.Counter(lst).most_common()]
[['c', 'c', 'c', 'c'], ['a', 'a', 'a'], ['b', 'b']]
like image 24
Josep Valls Avatar answered Nov 24 '25 00:11

Josep Valls