# Python – Group similar elements into Matrix

• Last Updated : 30 Dec, 2020

Sometimes, while working with Python Matrix, we can have a problem in which we need to perform grouping of all the elements with are same. This kind of problem can have application in data domains. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [1, 3, 4, 4, 2, 3]
Output : [[1], [2], [3, 3], [4, 4]]

Input : test_list = [1, 3, 4, 2]
Output : [[1], [2], [3], [4]]

Method #1 : Using list comprehension + `groupby()`
The combination of above functions provide possible solution to this problem. In this, we perform task of grouping using groupby() and list comprehension assists in iteration.

 `# Python3 code to demonstrate working of ``# Group similar elements into Matrix``# Using list comprehension + groupby()``from` `itertools ``import` `groupby`` ` `# initializing list``test_list ``=` `[``1``, ``3``, ``5``, ``1``, ``3``, ``2``, ``5``, ``4``, ``2``]`` ` `# printing original list ``print``(``"The original list : "` `+` `str``(test_list))`` ` `# Group similar elements into Matrix``# Using list comprehension + groupby()``res ``=` `[``list``(val) ``for` `key, val ``in` `groupby(``sorted``(test_list))]  ``         ` `# printing result ``print``(``"Matrix after grouping : "` `+` `str``(res))`
Output :
```The original list : [1, 3, 5, 1, 3, 2, 5, 4, 2]
Matrix after grouping : [[1, 1], [2, 2], [3, 3], [4], [5, 5]]
```

Method #2 : Using list comprehension + `Counter()`
This is yet another approach to this problem. In this, we get the values along with it’s frequency using Counter() and then employ list comprehension to multiply each element with frequency to get duplicates

 `# Python3 code to demonstrate working of ``# Group similar elements into Matrix``# Using list comprehension + Counter()``from` `collections ``import` `Counter`` ` `# initializing list``test_list ``=` `[``1``, ``3``, ``5``, ``1``, ``3``, ``2``, ``5``, ``4``, ``2``]`` ` `# printing original list ``print``(``"The original list : "` `+` `str``(test_list))`` ` `# Group similar elements into Matrix``# Using list comprehension + Counter()``temp ``=` `Counter(test_list)``res ``=` `[[key] ``*` `val ``for` `key, val ``in` `temp.items()]`` ` `# printing result ``print``(``"Matrix after grouping : "` `+` `str``(res))`
Output :
```The original list : [1, 3, 5, 1, 3, 2, 5, 4, 2]
Matrix after grouping : [[1, 1], [2, 2], [3, 3], [4], [5, 5]]
```

My Personal Notes arrow_drop_up