2

So basically for example of you have a list like:

l = ['a','b','a','b','c','c']

The output should be:

[['a','a'],['b','b'],['c','c']]

So basically put together the values that are duplicated into a list,

I tried:

l = ['a','b','a','b','c','c']
it=iter(sorted(l))
next(it)
new_l=[]
for i in sorted(l):
   new_l.append([])
   if next(it,None)==i:
      new_l[-1].append(i)
   else:
      new_l.append([])

But doesn't work, and if it does work it is not gonna be efficient

0

8 Answers 8

4

Use collections.Counter:

from collections import Counter

l = ['a','b','a','b','c','c']
c = Counter(l)

print([[x] * y for x, y in c.items()])
# [['a', 'a'], ['b', 'b'], ['c', 'c']]
3
  • 1
    Works too, nice Oct 12, 2018 at 8:56
  • 3
    This is the best solution. Easy to read and does not require sorting (if you use a Python version where dicts remember insertion order).
    – timgeb
    Oct 12, 2018 at 9:02
  • @timgeb Agreed! Although of course sorting and retaining the insertion order and not always going to produce the same output (although they do for this data); don't know what the OP wants actually for sure Oct 12, 2018 at 9:25
4

You can use collections.Counter:

from collections import Counter
[[k] * c for k, c in Counter(l).items()]

This returns:

[['a', 'a'], ['b', 'b'], ['c', 'c']]

%%timeit comparison

  • Given a sample dataset of 100000 values, this answer is the fastest approach.

enter image description here

2
  • 1
    Works too, nice Oct 12, 2018 at 8:56
  • 3
    Note that Counter() has an average time complexity of O(n).
    – blhsing
    Oct 12, 2018 at 9:00
3

Sort the list then use itertools.groupby:

>>> from itertools import groupby
>>> l = ['a','b','a','b','c','c']
>>> [list(g) for _, g in groupby(sorted(l))]
[['a', 'a'], ['b', 'b'], ['c', 'c']]

EDIT: this is probably not the fastest approach, sorting is O(n log n) time complexity for the average case and not required for all solutions (see the comments)

9
  • 1
    This requires an average time complexity of O(n log n), however.
    – blhsing
    Oct 12, 2018 at 8:54
  • 1
    @blhsing Yes, I know, I'm not actually sure this is the best solution it was just my first thought (one needs to be quick on SO), I will defer judgement to a timeit benchmark Oct 12, 2018 at 8:56
  • 1
    @Chris_Rands It's known that Python's sorted function has an average time complexity of O(n log n).
    – blhsing
    Oct 12, 2018 at 8:57
  • 1
    @blhsing yes you just said that, I agree :) Oct 12, 2018 at 9:01
  • 2
    @U9-Forward Thanks but I'm not convinced this is the best way, Austin or Blhsing's solutions might be faster, and will retain the order if the OrderedCounter recipe is added Oct 12, 2018 at 9:08
1

Another approach is to use zip method.

l = ['a','b','a','b','c','c','b','c', 'a']
l = sorted(l)
grouped = [list(item) for item in list(zip(*[iter(l)] * l.count(l[0])))]

Output

[['a', 'a', 'a'], ['b', 'b', 'b'], ['c', 'c', 'c']]
1
  • Works too, nice Oct 12, 2018 at 9:02
1

Here's a functional solution via itertools.groupby. As it requires sorting, this will have time complexity O(n log n).

from itertools import groupby
from operator import itemgetter

L = ['a','b','a','b','c','c']

res = list(map(list, map(itemgetter(1), groupby(sorted(L)))))

[['a', 'a'], ['b', 'b'], ['c', 'c']]

The syntax is cumbersome since Python does not offer native function composition. This is supported by 3rd party library toolz:

from toolz import compose

foo = compose(list, itemgetter(1))
res = list(map(foo, groupby(sorted(L))))
1
  • 1
    Works too, nice Oct 12, 2018 at 8:56
1

My solution using list comprehension would be (l is a list):

[l.count(x) * [x] for x in set(l)]
  • set(l) will retrieve all the element which appears in l, without duplicates
  • l.count(x) will return the number of times a specific element x appears in a given list l
  • the * operator creates a new list with the elements in a list (in this case, [x]) repeated the specified number of times (in this case, l.count(x) is the number of times)
0
0
l = ['a','b','a','b','c','c']

want = []
for i in set(l):
    want.append(list(filter(lambda x: x == i, l)))
print(want)    
4
  • 1
    time complexity O(n**2)
    – timgeb
    Oct 12, 2018 at 9:08
  • Works too, nice Oct 12, 2018 at 9:21
  • Timgeb you are right, but maybe size/speed des not matter. Oct 12, 2018 at 12:48
  • 1
    While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find how to write a good answer very helpful. Please edit your answer.
    – hellow
    Oct 16, 2018 at 7:36
-1

Probably not the most efficient, but this is understandable:

l = ['a','b','a','b','c','c']
dict = {}
for i in l:
    if dict[i]:
        dict[i] += 1
    else:
         dict[i] = 1

new = []
for key in list(dict.keys()):
    new.append([key] * dict[key])
1
  • This results in a KeyError Also, do not use built-in python functions (dict) as a variable name. Oct 14, 2021 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.