Python – Maximum length consecutive positive elements

Sometimes, while working with Python lists, we can have a problem in which we monitor a sequence and we need to find what was the maximum length when just positive elements occur. 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 = [4, 5, 4, 1, 7, 2, 5, 6, -2, -9, -10]
Output : 8

Input : test_list = [4, -5, -4, -1, -7, 2, 5, -6, -2, -9, -10]
Output : 2

Method #1 : Using loop
This is one of the way in which we perform this task. In this brute force way, we iterate for all the elements and keep on updating max, whenever the positive elements chain is broken.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Maximum length consecutive positive elements
# Using loop
  
# initializing list
test_list = [4, 5, -4, -1, -7, 2, 5, 6, -2, -9, -10]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Maximum length consecutive positive elements
# Using loop
counter = 0           
max_score = 1
for ele in test_list:
    if ele > 0:
        counter += 1
    else:
        if(counter > 0):
            max_score = max(max_score, counter)
        counter = 0
if(counter > 0):
        max_score = max(max_score, counter)
  
# printing result 
print("Maximum elements run length : " + str(max_score))

chevron_right


Output :



The original list : [4, 5, -4, -1, -7, 2, 5, 6, -2, -9, -10]
Maximum elements run length : 3

 

Method #2 : Using groupby() + defaultDict() + max()
The combination of above functions can be used to solve this problem. In this, we perform the task of grouping using groupby(), we can perform the task of finding max of both negative and positive maximum run by grouping them against different keys, and using max() to find maximum of list at end.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Maximum length consecutive positive elements
# Using groupby() + defaultDict() + max()
from collections import defaultdict
from itertools import groupby
  
# initializing list
test_list = [4, 5, -4, -1, -7, 2, 5, 6, -2, -9, -10]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Maximum length consecutive positive elements
# Using groupby() + defaultDict() + max()
counter = defaultdict(list)
for key, val in groupby(test_list, lambda ele: "plus" if ele >= 0 else "minus"):
    counter[key].append(len(list(val)))
res = []
for key in ('plus', 'minus'):
    res.append(counter[key])
  
# printing result 
print("Maximum elements run length : " + str(max(res[0])))
print("Maximum negative elements run length : " + str(max(res[1])))

chevron_right


Output :

The original list : [4, 5, -4, -1, -7, 2, 5, 6, -2, -9, -10]
Maximum elements run length : 3
Maximum negative elements run length : 3



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.