Python – Extract elements with Range consecutive occurrences

Sometimes while working with data, we can have a problem in which we need to select some of the elements that occur range times consecutively. This problem can occur in many domains. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using groupby() + list comprehension
This task can be performed using above functionalities. In this, we group all the numbers that are occurring range consecutively. We iterate the list using list comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Extract elements with Range consecutive occurrences
# using groupby() + list comprehension
from itertools import groupby
  
# initialize list 
test_list = [1, 1, 4, 5, 5, 6, 7, 7, 7, 8, 8, 8, 8]
  
# printing original list 
print("The original list : " + str(test_list))
  
# initialize strt, end 
strt, end = 2, 3
  
# Extract elements with Range consecutive occurrences
# using groupby() + list comprehension
res1 = [i for i, j in groupby(test_list) if len(list(j)) <= end]
res2 = [i for i, j in groupby(test_list) if len(list(j)) >= strt]
res = list(set(res1) & set(res2))
  
# printing result
print("The range consecutive elements are : " + str(res))

chevron_right


Output :

The original list : [1, 1, 4, 5, 5, 6, 7, 7, 7, 8, 8, 8, 8]
The range consecutive elements are : [1, 5, 7]

 

Method #2 : Using list comprehension + slice() + groupby()
This task can also be performed using above functions. In this, we just perform grouping in similar way as above but the way we extract consecutive elements is by slice().



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Extract elements with Range consecutive occurrences
# using groupby() + list comprehension + islice()
from itertools import groupby, islice
  
# initialize list 
test_list = [1, 1, 4, 5, 5, 6, 7, 7, 7, 8, 8, 8]
  
# printing original list 
print("The original list : " + str(test_list))
  
# initialize strt, end 
strt, end = 2, 3
  
# Extract elements with Range consecutive occurrences
# using groupby() + list comprehension + islice()
res1 = [i for i, j in groupby(test_list) if len(list(islice(j, 0, strt))) >= strt]
res2 = [i for i, j in groupby(test_list) if len(list(islice(j, 0, end))) <= end]
res = list(set(res1) & set(res2))
  
# printing result
print("The range consecutive elements are : " + str(res))

chevron_right


Output :

The original list : [1, 1, 4, 5, 5, 6, 7, 7, 7, 8, 8, 8]
The range consecutive elements are : [1, 5, 7, 8]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




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.