How to group identical values from a list into their own lists?

  • A+
Category:Languages

Say I have a list [2, 3, 7, 2, 3, 8, 7, 3]

I would like to produce lists that contain identical values from the list above.

Expected Output something like:

[2, 2] [3, 3, 3] [7, 7] [8] 

The order that these lists are produced doesn't matter.

 


Try this

l = [2, 3, 7, 2, 3, 8, 7, 3] for i in set(l):    print([i]*l.count(i)) 

Output:

[8] [2, 2] [3, 3, 3] [7, 7] 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: