Group by consecutive elements of a list in Python

from itertools import groupby

a = [1, 2, 3, 2, 2, 2, 3, 3, 4, 4, 4, 3, 1, 1]
a = sorted(a)

#Group contiguous elements
for key, group in groupby(a):
    print list(group)

Execution result

[1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
[4, 4, 4]

Recommended Posts

Group by consecutive elements of a list in Python
Get the number of specific elements in a python list
Display a list of alphabets in Python 3
Make a copy of the list in Python
Rewriting elements in a loop of lists (Python)
Sort list elements in a specified order in Python
[Python] Manipulating elements in a list (array) [Sort]
Getting list elements in Python
[Python] Manipulation of elements in list (array) [Add / Delete]
[Python] Outputs all combinations of elements in the list
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
[python] Manage functions in a list
Delete multiple elements in python list
Generate a list of consecutive characters
[Python] How to make a list of character strings character by character
How to get a list of built-in exceptions in python
How to check in Python if one of the elements of a list is in another list
[Python] A memo of frequently used phrases (by myself) in Python scripts
A simple data analysis of Bitcoin provided by CoinMetrics in Python
Try to get a list of breaking news threads in Python.
Python> Get a list of files in multiple directories> Use glob | Sort by modification time
[Python] Display only the elements of the list side by side [Vertical, horizontal]
Read the standard output of a subprocess line by line in Python
[python] Get a list of instance variables
Summary of built-in methods in Python list
[Python] Get a list of folders only
Get a list of files in a folder with python without a path
Get a list of packages installed in your current environment with python
[Python] How to put any number of standard inputs in a list
Receive a list of the results of parallel processing in Python with starmap
How to format a list of dictionaries (or instances) well in Python
Draw a graph of a quadratic function in Python
[Python] Sort the list of pathlib.Path in natural sort
How to clear tuples in a list (Python)
[python] Create a list of various character types
Get the caller of a function in Python
Match the distribution of each group in Python
A memorandum of extraction by python bs4 request
Get only the subclass elements in a list
View drug reviews using a list in Python
Get a list of Qiita likes by scraping
Make a joyplot-like plot of R in python
Output in the form of a python array
Get a glimpse of machine learning in Python
Search by the value of the instance in the list
Randomly select elements from list (array) in python
A well-prepared record of data analysis in Python
How to remove duplicate elements in Python3 list
Sorted list in Python
List of python modules
Sorted list in Python
Filter List in Python
List find in Python
How to pass the execution result of a shell command in a list in Python
Basic data frame operations written by beginners in a week of learning Python
How to get a list of files in the same directory with python
A list of stumbling blocks in Django's image upload
List of posts related to optimization by Python to docker
A collection of code often used in personal Python
Add a list of numpy library functions little by little --a
[python] Get the list of classes defined in the module