Use itertools.groupby
:
from itertools import groupby
N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
print([list(j) for i, j in groupby(N)])
Output:
[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]
Side note: Prevent from using global variable when you don’t need to.
More Related Contents:
- What is the difference between Python’s list methods append and extend?
- How do you remove duplicates from a list whilst preserving order?
- Removing duplicates in lists
- Why does append() always return None in Python? [duplicate]
- How do I find the duplicates in a list and create another list with them?
- Removing elements that have consecutive duplicates
- Python append() vs. + operator on lists, why do these give different results?
- Why does foo.append(bar) affect all elements in a list of lists?
- How do I check if there are duplicates in a flat list?
- What is the syntax to insert one list into another list in python?
- Appending item to lists within a list comprehension
- How do I remove duplicates from a list, while preserving order?
- TypeError: unhashable type: ‘list’ when using built-in set function
- Appending a dictionary to a list in a loop
- Remove duplicates and original from list
- Duplicate each member in a list [duplicate]
- How to allow list append() method to return the new list
- Python: Remove Duplicate Items from Nested list
- Is there a way to circumvent Python list.append() becoming progressively slower in a loop as the list grows?
- Python: Rename duplicates in list with progressive numbers without sorting list
- What’s the difference between plus and append in python for list manipulation? [duplicate]
- python remove duplicates from 2 lists
- Python The appended element in the list changes as its original variable changes
- Removing item from list causes the list to become NoneType
- How to unzip a list of tuples into individual lists? [duplicate]
- Writing a list to a file with Python, with newlines
- How can I convert each item in the list to string, for the purpose of joining them? [duplicate]
- How can I initialize a dictionary of distinct empty lists in Python?
- How to concatenate two dataframes without duplicates?
- smartest way to join two lists into a formatted string