# Python – Group similar elements into Matrix

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 : [, , [3, 3], [4, 4]]

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

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], , [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], , [5, 5]]
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.