How to Count Consecutive Elements in a Python List: 5 Clever Solutions
This article examines a common Python list problem—counting consecutive identical elements—and presents five distinct code implementations, each explained with step‑by‑step logic and visual output to help readers master the technique.
1. Introduction
A follower asked how to process a Python list to count consecutive identical values. The original data were:
a = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1]
b = [0, 1, 2, 0, 1, 2, 0, 1, 0, 0, 1, 2, 3, 4, 5]The goal is to generate a result list where each element records how many times the current value has appeared consecutively up to that position.
2. Implementation
Five community members shared their solutions.
Method 1 (by 瑜亮老师)
list1=[1,1,1,0,0,0,1,1,0,1,1,1,1,1,1]
result=[0]
flag=0
for i in range(1,len(list1)):
if list1[i]==list1[i-1]:
flag+=1
else:
flag=0
result.append(flag)
print(result)This approach uses a flag that increments when the current element matches the previous one and resets otherwise.
Method 2 (by 绅)
list1 = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1]
list2 = []
l = 0
for i in range(len(list1)):
l = l + 1 if list1[i] == list1[i-1] and i != 0 else 0
list2.append(l)
print(list2)This version condenses the logic into a single line inside the loop.
Method 3 (by 逸)
list1 = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1]
result = [0] * len(list1)
result[0] = 0
for i in range(1, len(list1)):
if list1[i] != list1[i-1]:
result[i] = 0
else:
result[i] = result[i-1] + 1
print(result)This method pre‑allocates the result list and updates each position based on the previous count.
Method 4 (by 月神)
list1 = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1]
result = [0] * len(list1)
pre_num = 0
for num in range(len(list1)):
print(num, pre_num)
if list1[num] != list1[pre_num]:
pre_num = num
result[num] = num - pre_num
print(result)
print(result)Here the algorithm tracks the index of the last change (pre_num) and computes the consecutive count as the distance from that index.
Method 5 (by 布达佩斯的永恒)
from functools import reduce
from itertools import groupby
list1 = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1]
ret = reduce(lambda x, y: x + y, [[i for i in range(len(list(num)))] for item, num in groupby(list1)])
print(ret)This solution leverages itertools.groupby and functools.reduce to build the consecutive counts in a functional style.
3. Summary
The article presented five practical ways to compute consecutive element counts in a Python list, ranging from straightforward loops with a flag variable to more compact one‑liners and functional programming techniques, giving readers multiple tools to solve similar list‑processing challenges.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
